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

在Android上通过whatsapp从我的应用程序共享pdf文件,android,pdf,whatsapp,Android,Pdf,Whatsapp,我正在尝试将pdf文件从我的应用程序发送到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.putE

我正在尝试将pdf文件从我的应用程序发送到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);
它打开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);
代码

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

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

这在技术上是错误的,如果有人有WhatsApp业务或想在gmail上共享文件,那么就用这个

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);
Intent shareIntent=新的意图(Intent.ACTION\u SEND);
shareIntent.setType(“文本/普通”);
shareIntent.putExtra(Intent.EXTRA\u TEXT,\u TEXT);
shareIntent.putExtra(Intent.EXTRA_流,Uri.parse(_文件));
startActivity(Intent.createChooser(shareIntent,“Share”));
在这种情况下,你只需添加文本和文件
附加的文本将成为gmail的主题,如果你在WhatsApp上共享图像,那么文本将成为图像标题,如果有人有同样的问题,这里是答案。获取错误
存储在我的设备内部存储器下载文件夹中的PDF文件不支持文件格式
。如何发送到特定的手机号码?
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);