Gmail不会发送通过意图共享的文件,但会显示“无效附件”错误

时间:2019-07-03 19:47:59

标签: android gmail attachment android-fileprovider

我正在尝试通过ACTION_SEND意图共享存储在getExternalFilesDir()中的文件。我使用FileProvider授予权限并创建uri。弹出的选择器包括Signal,Gmail和云端硬盘。如果我选择云端硬盘,则操作成功,并且文件已正确上传到我的帐户。如果我选择信号,它会成功,并且我的朋友会获取文件。如果我选择Gmail,则该操作似乎成功,并且我看到一条带有正确名称和大小的附件的邮件。我填写“收件人”字段,然后单击发送,作曲家消失了,但是没有发送或保存任何消息,并且出现logcat错误:> java.lang.IllegalStateException:在发送草稿时发现无效的附件。

这是清单:

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

这是provider_paths:

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-files-path
        name="data"
        path="."/>
</paths>

这是在MainActivity中构建和启动意图的地方:

     val uri = getUriForFile(
                    applicationContext,
                    "${BuildConfig.APPLICATION_ID}.fileprovider",
                    file
            )

            Log.d(TAG, "send uri: " + uri)


            Intent(Intent.ACTION_SEND)
                    .apply {
                        //setType("text/plain") // works for drive, but fails for email
                        setType("application/zip")
                        putExtra(Intent.EXTRA_STREAM, uri)
                        putExtra(Intent.EXTRA_SUBJECT, "Armonitor Record:" + file.nameWithoutExtension)
                        putExtra(Intent.EXTRA_TEXT, "See attached...")
                        setFlags(FLAG_GRANT_READ_URI_PERMISSION.or( FLAG_GRANT_WRITE_URI_PERMISSION))
                    }
                    .also {
                        startActivity(Intent.createChooser(it, "share file with"));
                    }

我知道fileprovider权限正在起作用,因为当我破坏它时,在显示选择器之前会出现错误。我尝试手动向该意图的任何使用者授予权限,以防该权限仅适用于setData uri。我尝试了各种文件类型,包括“应用程序/八进制”,“文本/纯文本”,“消息/ rfc822”。我尝试发送的文件在0.2到38 kB之间,并且我尝试过.txt和.zip文件。我确实注意到,如果我将“从”地址更改为其他gmail帐户,则附件将完全消失。无需附件即可成功发送电子邮件。我的手机上没有配置其他电子邮件客户端。

这是gmail发出的logcat消息,试图发送一个类型为

2019-07-05 11:29:22.944 11320-13752 W/Gmail: ComposeActivity:Failed to retrieve reference message with uri=null.
2019-07-05 11:29:22.980 2013-2107 I/LaunchCheckinHandler: Displayed com.google.android.gm/.ComposeActivityGmailExternal,wp,ca,316
2019-07-05 11:29:22.981 2013-2107 I/ActivityManager: Displayed com.google.android.gm/.ComposeActivityGmailExternal: +312ms
2019-07-05 11:29:22.988 11320-11320 W/Gmail: Gmail:No collectionId found for event forward
2019-07-05 11:29:22.988 11320-11320 W/Gmail: Gmail:No itemId found for event forward
2019-07-05 11:29:23.011 11320-11320 W/Gmail: Gmail:No collectionId found for event forward
2019-07-05 11:29:23.011 11320-11320 W/Gmail: Gmail:No itemId found for event forward
2019-07-05 11:29:23.016 11320-11320 W/Gmail: ComposeActivity:b/119949571:In finishSetup.
2019-07-05 11:29:23.019 11320-11320 W/Gmail: Gmail:b/119949571:loading bodyWebView with template emit size of 1898.
2019-07-05 11:29:23.114 11320-11320 E/Gmail: Gmail:EditWebView JS Console: b/119949571:draft.editor.onLoad; source: file:///android_asset/draft_editor_gmail_compiled.js at 87
2019-07-05 11:29:23.121 11320-11320 W/Gmail: Gmail:No collectionId found for event forward
2019-07-05 11:29:23.121 11320-11320 W/Gmail: Gmail:No itemId found for event forward
2019-07-05 11:29:23.124 11320-11320 E/Gmail: Gmail:EditWebView JS Console: b/119949571:draft.editor.onLoad is finished; source: file:///android_asset/draft_editor_gmail_compiled.js at 88
2019-07-05 11:29:23.149 11320-11320 W/Gmail: Gmail:No collectionId found for event forward
2019-07-05 11:29:23.149 11320-11320 W/Gmail: Gmail:No itemId found for event forward
2019-07-05 11:29:26.175 11320-11320 W/Gmail: Gmail:No collectionId found for event forward
2019-07-05 11:29:26.175 11320-11320 W/Gmail: Gmail:No itemId found for event forward
2019-07-05 11:29:37.855 11320-13837 W/Gmail: Gmail:No collectionId found for event forward
2019-07-05 11:29:37.856 11320-13837 W/Gmail: Gmail:No itemId found for event forward
2019-07-05 11:29:37.858 11320-13837 W/Gmail: Gmail:No collectionId found for event forward
2019-07-05 11:29:37.858 11320-13837 W/Gmail: Gmail:No itemId found for event forward
2019-07-05 11:29:37.893 11320-11320 E/Gmail: Gmail:Compose Event 1 failed with reason=1
2019-07-05 11:29:37.909 11320-13837 E/Gmail: sapishim:Exception in saveOrSendDraft: method=send_message
    java.util.concurrent.ExecutionException: java.lang.IllegalStateException: Found invalid attachments while sending the draft
        at afiw.a(SourceFile:3)
        at afiw.get(SourceFile:7)
        at gch.b(SourceFile:1)
        at com.google.android.gm.sapi.SapiUiProvider.call(SourceFile:32)
        at android.content.ContentProvider$Transport.call(ContentProvider.java:403)
        at android.content.ContentResolver.call(ContentResolver.java:1756)
        at din.a(SourceFile:109)
        at din.a(SourceFile:602)
        at doa.run(SourceFile:11)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at ghw.dispatchMessage(SourceFile:1)
        at android.os.Looper.loop(Looper.java:193)
        at android.os.HandlerThread.run(HandlerThread.java:65)
     Caused by: java.lang.IllegalStateException: Found invalid attachments while sending the draft
        at jbx.a(Unknown Source:4)
        at afjm.a(SourceFile:2)
        at afjj.run(SourceFile:6)
        at aflq.run(Unknown Source:7)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:458)
        at addr.run(SourceFile:2)
        at abql.run(Unknown Source:3)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6912)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:860)
2019-07-05 11:29:37.910 11320-13837 E/Gmail: Gmail:Draft send failed with reason=11
2019-07-05 11:29:37.943 11320-11320 W/Gmail: Gmail:No collectionId found for event forward
2019-07-05 11:29:37.943 11320-11320 W/Gmail: Gmail:No itemId found for event forward

Gmail认为“无效附件”是什么?如何通过Gmail共享这些文件?

我听说gmail设置了允许的附件类型和大小的设置,但是我不确定如何找到它们,或者这是否是这里发生的事情。

感谢您可能有任何想法。

2 个答案:

答案 0 :(得分:0)

好吧,这可能太愚蠢了,无法提供帮助,但是我确实解决了这个问题。就是我的provider_paths文件仅列出

<external-files-path
    name="data"
    path="."/>

我将zip文件保存在一个子文件夹中,所以我需要添加

<external-files-path
    name="zip"
    path="./zip" />

我不知道为什么它可以通过Signal和Drive共享,但是Gmail更挑剔。但现在它对所有人都有效,是的!

答案 1 :(得分:0)

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

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

原因是 Intent.FLAG_GRANT_READ_URI_PERMISSION 仅适用于 Intent.dataIntent.clipData,不适用于额外的,除非已明确授予 uri 权限(不方便),或已授予还添加一个 ClipData