Android 无法使用文件提供程序以编程方式将文件附加到电子邮件目的

Android 无法使用文件提供程序以编程方式将文件附加到电子邮件目的,android,android-fileprovider,Android,Android Fileprovider,我正在创建一封电子邮件并附加一个文件(使用文件提供商),但Gmail客户端报告“无法附加文件” 我以前用过这个,但现在坏了。我没有改变我的代码(我知道),但是更新了操作系统和Gmail客户端 // Build email Intent Uri mailToUri = Uri.fromParts("mailto", "", null); Intent emailIntent = new Intent(Intent.ACTION_SEND, mailToUri); emailIntent.setTy

我正在创建一封电子邮件并附加一个文件(使用文件提供商),但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)
Gmail客户端提供带有主题和正文的电子邮件草稿,但报告“无法附加文件”

我还尝试了使用所示的其他方法来创建Uri。在文件提供程序成为强制性之前,第一个应用程序已经开始工作。第二个失败的方式与上述代码相同

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
使用“content://”Uri会报告类似的错误,但以下行除外:

fwt:调用openAssetFileDescriptor时,AssetFileDescriptor为空。

[舱单剪报]

[文件\u provider\u path.xml]

看起来gmail找不到您试图附加的文件。 它说:“打开AssetFileDescriptor时FileNotFoundException”

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

File attachFile = new File(aDirectory, aFileName);
进一步说明:

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

    • 对我来说,在我投下createNewFile之后,它就起作用了

      // Build the attachment URI
      File attachFile = new File(aDirectory, aFileName);
      attachFile.createNewFile();
      

      如果还将其添加为
      ClipData
      ,则权限授予将成功:

      yourIntent.clipData = ClipData.newUri(context.contentResolver, fileName, contentUri)
      

      罪魁祸首是
      Intent.FLAG\u GRANT\u READ\u URI\u PERMISSION
      将仅适用于
      Intent.data
      Intent.clipData
      ,而不适用于额外的权限,除非已明确授予URI权限(不方便),或者已通过添加看起来正常的
      clipData
      。这可能是Gmail中的一个bug。如果尚未执行此操作,请编写一个插入指令的测试,以确认您可以读取
      文件提供程序提供的内容。结果表明,路径名中存在细微错误(因此错误消息确实是正确的)。如果Gmail报告了它找不到的路径,它会有很大帮助。我想空间不会是个问题,因为我有没有空间的文件,但问题仍然存在