簡體   English   中英

如何在 android 中將 pdf 附加到意圖發送(到 email、保管箱等)

[英]How to attach a pdf to intent to send (to email, dropbox, etc) in android

嘿,所以我將我的 pdf 保存在外部數據存儲中。 例如:

Environment.getExternalStorageDirectory().getPath() + "/file.pdf"

然后,我嘗試將其附加到發送意圖:

        File attachment = this.getFileStreamPath(fileDirectory + "/" + fileName);


        Uri uri = Uri.fromFile(attachment);

        Intent emailIntent = new Intent(Intent.ACTION_SEND);
        emailIntent.setDataAndType(Uri.parse("mailto:"), "text/plain"); // I have also tried "application/pdf"

        emailIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

        emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Calc PDF Report");
        emailIntent.putExtra(Intent.EXTRA_TEXT, " PDF Report");

        emailIntent.putExtra(Intent.EXTRA_STREAM, uri);
        startActivity(Intent.createChooser(emailIntent, "Send mail..."));
        finish();

我得到了錯誤:

 Caused by: java.lang.IllegalArgumentException: File /storage/emulated/0/file.pdf contains a path separator

我認為這是我保存文件的問題,但找不到任何最新的示例。

要使用 Intent 將文件共享為 email 附件,您需要使用 FileProvider。

/**
 * Generate file content and returns uri file
 */
public static Uri generateFile(Context context) {
  File pdfDirPath = new File(context.getFilesDir(), "pdfs");
  pdfDirPath.mkdirs();

  File file = new File(pdfDirPath, "attachment.pdf");
  file.deleteOnExit();

  Uri uri = FileProvider.getUriForFile(context, context.getPackageName() + ".file.provider", file);
  FileOutputStream os = null;
  try {
    Logger.info("Generate file " + file.getAbsolutePath());
    os = new FileOutputStream(file);
    document.writeTo(os);
    document.close();
    os.close();
  } catch (IOException e) {
    e.printStackTrace();
  }
  return uri;
}

private void share(Context context) {
  Uri uri = generateFile(context);

  final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
  emailIntent.setType("text/plain");

  emailIntent.putExtra(Intent.EXTRA_STREAM, uri);
  emailIntent.putExtra(EXTRA_SUBJECT, "Send something");
  emailIntent.putExtra(Intent.EXTRA_TEXT, "You receive attachment");
  emailIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

  startActivity(emailIntent);
}

在您的應用中添加文件提供程序定義:

AndroidManifest.xml

<application
  android:name=".DemaApplication"
  android:allowBackup="false"
  android:icon="@mipmap/ic_launcher"
  android:label="@string/app_name"
  android:roundIcon="@mipmap/ic_launcher_round"
  android:supportsRtl="true"
  android:theme="@style/AppTheme">

  <provider
    android:name="androidx.core.content.FileProvider"
    android:authorities="${applicationId}.file.provider"
    android:exported="false"
    android:grantUriPermissions="true">
    <meta-data
       android:name="android.support.FILE_PROVIDER_PATHS"
             android:resource="@xml/provider_paths" />
  </provider>
  ...
</application>
provider_path.xml

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <files-path
        name="internal_files"
        path="/"/>
    <!--<external-path name="external_files" path="./files"/>-->
</paths>

最后但並非最不重要的一點是,您需要指定文件提供程序路徑(您的文件在哪里)。 我希望這有幫助。 在這里,關於如何發送 email 和附件的官方文檔。

問題 #1: getFileStreamPath()不支持子目錄,並且與文件所在的外部存儲無關。

問題 #2: Uri.fromFile()在 Android 7.0 上不起作用,因為您的應用程序將因FileUriExposedException而崩潰。 要解決此問題和問題 #1,請使用FileProvider設置可用於EXTRA_STREAMcontent Uri

問題#3: ACTION_SEND不使用“數據” Uri (即,您的"mailto:"不應該在那里)。

問題 4:PDF 的 MIME 類型不是text/plain — 正如您的評論所指出的,使用application/pdf

問題 #5: getExternalStorageDirectory()在 Android 10 及更高版本上已棄用,您將無法在那里寫入文件。 考慮使用getExternaFilesDir(null) (在Context上調用)以獲得無需權限和更多 Android 操作系統版本的更好位置。

找不到任何最新的示例

該文檔涵蓋ACTION_SEND的使用

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM