在 Android 中通过 WhatsApp 发送 PDF

Sending PDF through WhatsApp in Android

我的意图是通过我的 application.What 通过 whatsapp 发送 PDF 文件,现在发生的是它没有打开 whatsapp appliation.Below 是我的代码:

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

据我了解,您不需要创建选择器,希望您能直接分享到 WhatsApp。

Whatsapp Documentation :
If you prefer to share directly to WhatsApp and bypass the system picker, you can do so by using setPackage in your intent.

WhatsApp Documentation

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

编辑

对于目标 Sdk 24 或更高版本,您需要使用 FileProvider class 才能使用特定文件。 按照列出的指示进行操作 .