如何从assets文件夹发送pdf文件

时间:2016-10-24 16:15:15

标签: android pdf

我无法通过电子邮件或Android上的WhasApp发送pdf文件。以下是我发送pdf文件的代码 - 它有什么问题?

 File file = new File("android.resource://beeonline.com.chromatography/raw/vendormumbai.pdf");

 Intent intent = new Intent(Intent.ACTION_SEND);
 intent.setDataAndType(Uri.fromFile(file), "application/pdf");
 intent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Sharing Mumbai  File...");
 intent.putExtra(android.content.Intent.EXTRA_TEXT, "Sharing File...");
 try {
      startActivity(Intent.createChooser(intent, "Share PDF file"));
 } catch (ActivityNotFoundException e) {
      Toast.makeText(ActivityshowVendorForm.this, "Error: Cannot open or share created PDF report.", Toast.LENGTH_SHORT).show();
 }

1 个答案:

答案 0 :(得分:0)

首先,您的问题标题是指资产。您的代码与资产无关。

其次,android.resource://mypackagename/raw/vendormumbai.pdf不是文件路径,就像https://stackoverflow.com/questions/40222995/how-to-send-the-pdf-file-from-the-assets-folder-via-share-option-in-mail-or-wats不是文件路径一样。您的值是Uri的字符串表示,指向原始资源(不是资产)。

欢迎您尝试更换:

File file = new File("android.resource://mypackagename/raw/vendormumbai.pdf");

Intent intent = new Intent(Intent.ACTION_SEND);
intent.setDataAndType(Uri.fromFile(file),"application/pdf");

使用:

Uri thing = Uri.parse("android.resource://mypackagename/raw/vendormumbai.pdf");

Intent intent = new Intent(Intent.ACTION_SEND);
intent.setDataAndType(thing,"application/pdf");

这应该更好,但android.resource方案并不常见,因此并非所有ACTION_SEND收件人都能识别它。此外,这假设您使用的是原始资源,而不是资产。

如果您确实想要从资源中提供PDF,或者您想要的应用不支持android.resource,则可以尝试my StreamProvider,其中提供ContentProvider个其他方面,可以使用content Uri来提供资源和原始资源。

或者,您可以实施自己的ContentProvider,可以assets/res/raw/投放该文件。

或者,您可以将资产(或原始资源)复制到本地文件(例如,getFilesDir()getCacheDir()),then use FileProvider to serve that local file