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

Suj*_*hta 10 pdf android share whatsapp

我尝试使用以下代码,但它没有附加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);
Run Code Online (Sandbox Code Playgroud)

KHA*_*LED 24

我有这个问题,我试图从资源文件夹打开一个pdf文件,我没有工作,但当我尝试从下载文件夹打开(例如),它实际上工作,这是一个例子:

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);      
Run Code Online (Sandbox Code Playgroud)


小智 16

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

第1步:在AndroidManifest.xml中的application tag下添加一个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>
Run Code Online (Sandbox Code Playgroud)

第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>
Run Code Online (Sandbox Code Playgroud)

第3步:最后一步是更改下面的代码行

Uri photoURI = Uri.fromFile(outputFile);
Run Code Online (Sandbox Code Playgroud)

Uri uri = FileProvider.getUriForFile(PdfRendererActivity.this, PdfRendererActivity.this.getPackageName() + ".provider", outputFile);
Run Code Online (Sandbox Code Playgroud)

第4步(可选):

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

intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Run Code Online (Sandbox Code Playgroud)

希望这会有所帮助:)

  • 对于较新版本的 SDK,将清单文件的 Provider 部分中的名称从 android:name="android.support.v4.content.FileProvider" 更改为 android:name="androidx.core.content.FileProvider"... (3认同)

小智 5

       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 
Run Code Online (Sandbox Code Playgroud)