无法使用 FileProvider 以编程方式将文件附加到电子邮件意图

T C*_*low 5 android android-fileprovider

我正在创建电子邮件意图并附加文件(使用 FileProvider),但 Gmail 客户端报告“无法附加文件”。

我以前有这个工作,但现在坏了。我没有更改我的代码(我知道),但已经更新了操作系统和 Gmail 客户端。

// Build email Intent
Uri mailToUri = Uri.fromParts("mailto", "", null);
Intent emailIntent = new Intent(Intent.ACTION_SEND, mailToUri);
emailIntent.setType("text/plain");
emailIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "A subject");
emailIntent.putExtra(Intent.EXTRA_TEXT, "A body");

// Build the attachment URI
File attachFile = new File(aDirectory, aFileName);
Uri attachmentUri = FileProvider.getUriForFile(this,
                        FILE_PROVIDER_AUTHORITY, attachFile);
emailIntent.putExtra(Intent.EXTRA_STREAM, attachmentUri);

// Start the share activity
startActivity(emailIntent);

// Alternative methods for creating the URI 
// Uri attachmentUri = Uri.parse("file://" 
//                   + attachFile.getAbsolutePath());
// Uri attachmentUri = Uri.parse("content://" 
//                   + FILE_PROVIDER_AUTHORITY
//                   + "/" + attachFileName)
Run Code Online (Sandbox Code Playgroud)

Gmail 客户端显示带有主题和正文的电子邮件草稿,但报告“无法附加文件”。

我还尝试使用显示的替代方法来创建 Uri。第一个用于在 FileProviders 成为强制性之前工作。第二个失败的方式与上述代码相同。

LOGCAT 中有一些 Gmail 错误消息:

08-27 17:15:39.463  8972  8972 W Gmail   : Gmail:No collectionId found for event forward
08-27 17:15:39.463  8972  8972 W Gmail   : Gmail:No itemId found for event forward
08-27 17:15:39.505  8972  8972 W Gmail   : Gmail:No collectionId found for event forward
08-27 17:15:39.505  8972  8972 W Gmail   : Gmail:No itemId found for event forward
08-27 17:15:39.514  8972  8972 W Gmail   : ComposeActivity:b/119949571:In finishSetup.
08-27 17:15:39.515  8972  8972 W Gmail   : Gmail:b/119949571:loading bodyWebView with template emit size of 2120.
08-27 17:15:39.576  8972  8972 E Gmail   : ComposeActivity:Error adding attachment
08-27 17:15:39.576  8972  8972 E Gmail   : fwt: FileNotFoundException when openAssetFileDescriptor.
08-27 17:15:39.576  8972  8972 E Gmail   :  at fww.a(SourceFile:7)
08-27 17:15:39.576  8972  8972 E Gmail   :  at fww.a(SourceFile:41)
08-27 17:15:39.576  8972  8972 E Gmail   :  at dfb.a(SourceFile:308)
08-27 17:15:39.576  8972  8972 E Gmail   :  at dhs.run(SourceFile:2)
08-27 17:15:39.576  8972  8972 E Gmail   :  at dfb.a(SourceFile:71)
08-27 17:15:39.576  8972  8972 E Gmail   :  at dfb.a(SourceFile:412)
08-27 17:15:39.576  8972  8972 E Gmail   :  at dfo.a(Unknown Source:9)
08-27 17:15:39.576  8972  8972 E Gmail   :  at adco.a(Unknown Source:6)
08-27 17:15:39.576  8972  8972 E Gmail   :  at afet.a(SourceFile:2)
08-27 17:15:39.576  8972  8972 E Gmail   :  at afeu.run(SourceFile:6)
08-27 17:15:39.576  8972  8972 E Gmail   :  at afgx.run(Unknown Source:7)
08-27 17:15:39.576  8972  8972 E Gmail   :  at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:458)
08-27 17:15:39.576  8972  8972 E Gmail   :  at adag.run(SourceFile:2)
08-27 17:15:39.576  8972  8972 E Gmail   :  at abno.run(Unknown Source:3)
08-27 17:15:39.576  8972  8972 E Gmail   :  at android.os.Handler.handleCallback(Handler.java:873)
08-27 17:15:39.576  8972  8972 E Gmail   :  at android.os.Handler.dispatchMessage(Handler.java:99)
08-27 17:15:39.576  8972  8972 E Gmail   :  at android.os.Looper.loop(Looper.java:193)
08-27 17:15:39.576  8972  8972 E Gmail   :  at android.app.ActivityThread.main(ActivityThread.java:6923)
08-27 17:15:39.576  8972  8972 E Gmail   :  at java.lang.reflect.Method.invoke(Native Method)
08-27 17:15:39.576  8972  8972 E Gmail   :  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
08-27 17:15:39.576  8972  8972 E Gmail   :  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:870)
08-27 17:15:39.653  8972  8972 W Gmail   : Gmail:No collectionId found for event forward
08-27 17:15:39.653  8972  8972 W Gmail   : Gmail:No itemId found for event forward
08-27 17:15:39.682  8972  8972 W Gmail   : Gmail:No collectionId found for event forward
08-27 17:15:39.682  8972  8972 W Gmail   : Gmail:No itemId found for event forward
08-27 17:15:39.842  8972  8972 E Gmail   : Gmail:EditWebView JS Console: b/119949571:draft.editor.onLoad; source: file:///android_asset/draft_editor_gmail_compiled.js at 87
08-27 17:15:39.917  8972  8972 E Gmail   : Gmail:EditWebView JS Console: b/119949571:draft.editor.onLoad is finished; source: file:///android_asset/draft_editor_gmail_compiled.js at 88
Run Code Online (Sandbox Code Playgroud)

使用 "content://" Uri 报告类似的错误,除了以下行: fwt: Null AssetFileDescriptor while calling openAssetFileDescriptor.

[清单片段] <provider android:name="android.support.v4.content.FileProvider" android:authorities="com.pentad.bridge.fileprovider" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_provider_paths"/> </provider>

[文件file_provider_paths.xml] <?xml version="1.0" encoding="utf-8"?> <paths xmlns:android="http://schemas.android.com/apk/res/android"> <external-path name="app_root" path="."/> </paths>

Hai*_*imS 0

Gmail 似乎找不到您尝试附加的文件。它说:“当 openAssetFileDescriptor 时出现 FileNotFoundException”

确保此处创建的文件具有现有文件的有效路径。

File attachFile = new File(aDirectory, aFileName);
Run Code Online (Sandbox Code Playgroud)

进一步说明:

  • FileProvider.getUriForFile() 是当今的最佳选择。也就是说,您的替代方法将不起作用。
  • 将 File 对象传递给 FileProvider.getUriForFile() 时,请确保该文件的下划线路径不包含任何 uri 前缀,例如"://file""://content"。它应该是一个简单的路径表示。例如:"/storage/emulated/0/myapp/myfile.jpg"。特别是,不要在路径中使用 URL 编码。例如,如果路径中有空格,请执行以下操作:"/storage/emulated/0/myapp/my attachments/myfile.jpg"而不是"/storage/emulated/0/myapp/my%20attachments/myfile.jpg"