android - 在 Android 中通过 WhatsApp 发送 PDF

标签 android whatsapp

我的目的是通过我的应用程序通过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.

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 类才能使用特定文件。 按照 here 列出的说明进行操作.

关于android - 在 Android 中通过 WhatsApp 发送 PDF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50907972/

相关文章:

android - 无法将原始文件复制到临时文件

java - 使用注释在空对象引用上设置文本

android - 如何将我的 Android 应用程序提供到我的网站以供用户下载

android - 电报、whatsapp、viber TCP 连接

android - 如何找出应用程序(Whats App)的使用时间?

android - 当应用程序在 android 中处于后台时继续播放流式音频

Android:在视频捕获期间在视频上添加颜色叠加效果

android - 如何将多张图片分享到 whatsapp 应用程序?

ios - 我的应用程序在 WhatsApp 的共享扩展中

ios - UIActivityViewController 错误 : Failed to determine whether URL is managed by a file provider