如何通过android中的whatsapp共享pdf和文本?

问题描述 投票:11回答:8

我尝试使用以下代码,但未附加pdf文件。

Intent sendIntent = new Intent();
        sendIntent.setAction(Intent.ACTION_SEND);
        sendIntent.putExtra(Intent.EXTRA_TEXT, message);
        sendIntent.setType("text/plain");
        if (isOnlyWhatsApp) {
            sendIntent.setPackage("com.whatsapp");

        }

        Uri uri = Uri.fromFile(attachment);
        sendIntent.putExtra(Intent.EXTRA_STREAM, uri);
        activity.startActivity(sendIntent);
android pdf share whatsapp
8个回答
24
投票

我有一个问题,我试图从assets文件夹打开一个pdf文件,但是我没有用,但是当我尝试从Download文件夹打开(例如)时,它确实起作用了,下面是一个例子:

File outputFile = new File(Environment.getExternalStoragePublicDirectory
    (Environment.DIRECTORY_DOWNLOADS), "example.pdf");
Uri uri = Uri.fromFile(outputFile);

Intent share = new Intent();
share.setAction(Intent.ACTION_SEND);
share.setType("application/pdf");
share.putExtra(Intent.EXTRA_STREAM, uri);
share.setPackage("com.whatsapp");

activity.startActivity(share);      

16
投票

请注意,如果您的targetSdkVersion为24或更高,我们必须使用FileProvider类来授予对特定文件或文件夹的访问权限,以使其他应用程序可以访问它们。

[步骤1]]在AndroidManifest.xml中的应用程序标签下添加一个FileProvider标签。

<provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="${applicationId}.provider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/provider_paths"/>
        </provider>

步骤2:

然后在res文件夹下的xml文件夹中创建provider_paths.xml文件。如果文件夹不存在,可能需要创建该文件夹。该文件的内容如下所示。它描述了我们希望使用名称external_files在根文件夹(path =“。”)上共享对外部存储的访问。

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="external_files" path="."/>
</paths>

步骤3:

最后一步是更改下面的代码行
Uri photoURI = Uri.fromFile(outputFile);

to

Uri uri = FileProvider.getUriForFile(PdfRendererActivity.this, PdfRendererActivity.this.getPackageName() + ".provider", outputFile);

第4步(可选)

如果使用意图使系统打开文件,则可能需要添加以下代码行:

intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

希望这会有所帮助:)


2
投票
       if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                pdfUri = FileProvider.getUriForFile(this, this.getPackageName() + ".provider", pdfFile);
            } else {
                pdfUri = Uri.fromFile(pdfFile);
            }
            Intent share = new Intent();
            share.setAction(Intent.ACTION_SEND);
            share.setType("application/pdf");
            share.putExtra(Intent.EXTRA_STREAM, pdfUri);
            startActivity(Intent.createChooser(share, "Share"));

If you are using Intent.createChooser then always open chooser 

1
投票

ACTION_VIEW


0
投票

尝试如下添加Intent.setType


0
投票

关于共享文本,下面您可以找到一个很好的例子,可以共享特定编号的文本!


0
投票

尝试以下代码


-2
投票

转到android中的文件管理器应用并打开它然后转到>>> data >>> data >>> com.whatsapp,然后>>> share_prefs打开com.whatsapp_preference.xml文件搜索并选择文件>>>> name = document pdf .... string>并保存此文件>>>设置>>>>应用程序>>>>什么应用程序>>>>后按并强制停止新再次打开whatsapp,然后尝试发送或共享您的文档

© www.soinside.com 2019 - 2024. All rights reserved.