【问题标题】:Gmail does not send file shared through intent, but gives "invalid attachments" errorGmail 不会发送通过意图共享的文件,但会出现“无效附件”错误
【发布时间】:2019-11-14 13:14:51
【问题描述】:

我正在尝试通过 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”的

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 设置了允许的附件类型和大小,但我不知道如何找到它们,也不知道这里是否发生了这种情况。

感谢您提出的任何想法。

【问题讨论】:

  • "我尝试了各种文件类型,包括 "application/octal"、"text/plain"、"message/rfc822"" - 您尝试共享的文件的实际类型是什么?请注意,application/octal 不是公认的 MIME 类型;你可能会想到application/octet-stream
  • 对不起,我就是这个意思。我想共享 zip 文件和文本文件。我尝试了两种文件类型的所有类型,以及“application/zip”
  • 如果您尝试通过 Gmail 共享并失败,Logcat 是否包含来自 Gmail 的任何提供任何线索的邮件?如果其他应用可以读取内容,并且您通过 MIME 类型准确描述内容,我不知道为什么 Gmail 会出现内容问题。

标签: android gmail attachment android-fileprovider


【解决方案1】:

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

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

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

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

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

【讨论】:

    【解决方案2】:

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

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

    原因是 Intent.FLAG_GRANT_READ_URI_PERMISSION 仅适用于 Intent.dataIntent.clipData,而不适用于 extras,除非已明确授予 uri 权限授予(不方便),或者已通过添加 @987654326 授予@。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-04-23
      • 2018-04-23
      • 2016-11-25
      • 2012-05-09
      • 1970-01-01
      • 2015-10-09
      • 2019-05-06
      相关资源
      最近更新 更多