通过 Whatsapp 从我的 Android 上的应用程序共享 pdf 文件

2024-03-18

我尝试将 pdf 文件从我的应用程序发送到 Whatsapp,这是代码, 但缺少一些东西!

它打开了 Whatsapp,我可以选择一个联系人,但它说“共享失败”!

代码

String PLACEHOLDER = "file:///android_asset/QUOT_2016_10(test).pdf";
File f = new File(PLACEHOLDER);
Uri uri = Uri.fromFile(f);

Intent share = new Intent();
share.setAction(Intent.ACTION_SEND);
share.putExtra(Intent.EXTRA_TEXT, "hi");
share.setPackage("com.whatsapp");

share.putExtra(Intent.EXTRA_STREAM, uri);
share.setType("application/pdf");

activity.startActivity(share);

我解决了这个问题,如果有人遇到同样的问题,这里是答案。问题是我试图从资产文件夹中打开 pdf,但它不起作用,如果尝试从下载文件夹中打开 pdf,它会起作用。最终正确的方法请参考下面的代码:

File outputFile = new File(Environment.getExternalStoragePublicDirectory
        (Environment.DIRECTORY_DOWNLOADS), "ref Number from Quotation.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);                                                
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

通过 Whatsapp 从我的 Android 上的应用程序共享 pdf 文件 的相关文章

随机推荐