从我的 Android 应用程序中通过 whatsapp 共享 pdf 文件

问题描述:

我尝试将 pdf 文件从我的应用程序发送到 whatsapp,这是代码,但是少了点什么!!

I am try to send pdf file from my app to whatsapp, and here is the code, but something missing!!

它打开whatsapp,我可以选择一个联系人,但它说共享失败"!

it opens whatsapp and i can choose a contact but it says "sharing failed"!

代码

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,它会起作用.最终正确方法请参考以下代码:

I figured out the problem, and here is the answer if somebody had the same issue. The problem was that I am trying to open the pdf from the asset folder which did n't work, and if would try to open the pdf from the download folder for example, it would work. Please refer to the the code below for the final correct way:

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);