在Android中通过WhatsApp发送PDF

在Android中通过WhatsApp发送PDF,android,whatsapp,Android,Whatsapp,我打算通过我的应用程序通过whatsapp发送一个PDF文件。现在发生的事情是它没有打开whatsapp应用程序。下面是我的代码: Uri uri = Uri.fromFile(pdfFile); Intent share = new Intent(Intent.ACTION_SEND); //share.setAction(Intent.ACTION_SEND); s

我打算通过我的应用程序通过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文档
如果您希望直接与WhatsApp共享并绕过系统选择器,则可以在您的意图中使用setPackage

编辑

对于目标Sdk 24或更高版本,您需要使用
FileProvider
类才能使用特定的文件。
按照列出的说明操作。

日志中是否有错误?日志中没有错误@Mweird,你的代码在我的虚拟机上运行得很好,你确定你的测试设备上安装了whatsApp吗?是的,已经安装了。这是我第一次尝试。不幸的是,这也不起作用。啊。。。该死的。。。您能否提供各自的日志条目?只是建议包含try-catch和print-all-exception…您的targetSdkVersion是什么?是24还是更高?Targetversion是26这个把戏完成了我的任务。请将此作为答案发布。我会接受的。谢谢
 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);