我尝试从我的应用程序发送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);