在 Android 上从我的应用程序通过 whatsapp 共享 pdf 文件
Share pdf file via whatsapp from my app on Android
我正在尝试将 pdf 文件从我的应用程序发送到 whatsapp,这是代码,
但是少了点东西!!
它会打开 whatsapp,我可以选择一个联系人,但它显示 "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,它会工作。最终正确的方法请参考下面的代码:
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);
File outputPath= new File(Environment.getExternalStorageDirectory() + "/MyPdf/example.pdf");//change with your path
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("application/pdf");
Uri fileUri = FileProvider.getUriForFile(getApplicationContext(),
getPackageName() + ".fileprovider", outputPath);
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
shareIntent.putExtra(Intent.EXTRA_STREAM, fileUri);
startActivity(Intent.createChooser(shareIntent, "Share it"));
这在技术上是错误的,如果有人有 WhatsApp 业务或想在 gmail 上共享文件然后使用这个...
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT, _text);
shareIntent.putExtra (Intent.EXTRA_STREAM, Uri.parse(_file));
startActivity(Intent.createChooser( shareIntent, "Share"));
在这里你只需要添加文本和文件
您附加的文字将成为 Gmail 中的主题,如果您在 WhatsApp 上分享图片,那么文字将成为图片标题
我正在尝试将 pdf 文件从我的应用程序发送到 whatsapp,这是代码, 但是少了点东西!!
它会打开 whatsapp,我可以选择一个联系人,但它显示 "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,它会工作。最终正确的方法请参考下面的代码:
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);
File outputPath= new File(Environment.getExternalStorageDirectory() + "/MyPdf/example.pdf");//change with your path
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("application/pdf");
Uri fileUri = FileProvider.getUriForFile(getApplicationContext(),
getPackageName() + ".fileprovider", outputPath);
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
shareIntent.putExtra(Intent.EXTRA_STREAM, fileUri);
startActivity(Intent.createChooser(shareIntent, "Share it"));
这在技术上是错误的,如果有人有 WhatsApp 业务或想在 gmail 上共享文件然后使用这个...
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT, _text);
shareIntent.putExtra (Intent.EXTRA_STREAM, Uri.parse(_file));
startActivity(Intent.createChooser( shareIntent, "Share"));
在这里你只需要添加文本和文件 您附加的文字将成为 Gmail 中的主题,如果您在 WhatsApp 上分享图片,那么文字将成为图片标题