首页 > 解决方案 > Gmail 不会发送通过意图共享的文件,但会出现“无效附件”错误

问题描述

我正在尝试通过 ACTION_SEND 意图共享存储在 getExternalFilesDir() 中的文件。我使用 FileProvider 来授予权限并创建 uri。弹出的选择器包括 Signal、Gmail 和 Drive。如果我选择云端硬盘,操作会成功,并且文件会正确上传到我的帐户。如果我选择信号,它会成功,我的朋友会得到文件。如果我选择 Gmail,则操作似乎成功,我会看到一条带有正确名称和大小的附件的消息。我填写“to”字段并点击发送,作曲家消失了,但没有发送或保存任何消息,并且出现 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"));
                    }

我知道文件提供者权限正在工作,因为当我中断它时,在显示选择器之前会出现错误。我尝试手动向意图的任何使用者授予权限,以防权限仅适用于 setData uri。我尝试过各种文件类型,包括“application/octal”、“text/plain”、“message/rfc822”。我尝试发送的文件在 0.2 到 38 kB 之间,我尝试过 .txt 和 .zip 文件。我确实注意到,如果我将“发件人”地址更改为不同的 gmail 帐户,附件会完全消失。没有附件的邮件可以发送成功。我的手机上没有配置其他电子邮件客户端。

这是来自 gmail 的 logcat 消息,用于尝试发送类型为“application/zip”的小于 1kB 的小型 zip 文件。较大的 zipfile (4kB) 的结果略有不同——撰写事件失败,但出现异常 1,但没有无效附件错误,并且电子邮件已保存并在没有附件的情况下发送:

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 设置了允许的附件类型和大小,但我不确定如何找到它们或者这是否是这里发生的事情。

感谢您的任何想法。

标签: androidgmailattachmentandroid-fileprovider

解决方案


好的,这可能太愚蠢了,没有帮助,但我确实解决了这个问题。那是我的 provider_paths 文件只列出

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

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

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

我不知道为什么它可以通过 Signal 和 Drive 共享,但 Gmail 更挑剔。但现在它适用于他们所有人,所以耶!


推荐阅读