我的目的是通过我的应用程序通过whatsapp发送PDF文件。现在发生的情况是它没有打开whatsapp应用程序。下面是我的代码:
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.
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
类才能使用特定文件。
按照 here 列出的说明进行操作.
关于android - 在 Android 中通过 WhatsApp 发送 PDF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50907972/