我正在尝试通过action_send intent共享存储在getexternalfilesdir((中的文件。我使用FileProvider授予许可并创建URI。弹出的选择器包括信号,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_path:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-files-path
name="data"
path="."/>
</paths>
这是构建意图的地方,并以主攻击性开始:
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权威正在工作,因为当我破坏它时,我会在显示chooser之前会出现错误。如果权限仅适用于setdata uri,我尝试向任何意图的消费者手动授予权限。我尝试了各种文件类型,包括"应用程序/八进制","文本/普通","消息/RFC822"。我试图发送的文件在0.2到38 kb之间,我尝试了.txt和.zip文件。我确实注意到,如果将"从"地址更改为其他Gmail帐户,则附件将完全消失。可以在没有附件的情况下成功发送电子邮件。我的手机上没有其他电子邮件客户端。
这是Gmail的logcat消息,以尝试发送带有类型=" application/zip"的小&lt; 1kb zip文件。较大的Zipfile(4KB(的结果略有不同 - 撰写事件失败了,但没有无效的附件错误,并且没有附件保存并发送了电子邮件:
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有针对允许类型和附件大小的设置,但是我不确定如何找到它们或是否在这里发生了什么。
感谢您可能拥有的任何想法。
好吧,这可能太愚蠢了,无法帮助您,但我确实解决了这个问题。是我的provider_paths文件仅列出
<external-files-path
name="data"
path="."/>
我将zip文件保存在子文件夹中,所以我需要添加
<external-files-path
name="zip"
path="./zip" />
我不知道为什么它可以通过信号和驱动器共享,但Gmail是选择的。但是现在它对他们所有人都有用,所以!
如果您还将其添加为ClipData
,则许可授予将成功起作用:
yourIntent.clipData = ClipData.newUri(context.contentResolver, fileName, contentUri)
原因是Intent.FLAG_GRANT_READ_URI_PERMISSION
仅适用于Intent.data
和Intent.clipData
,而不适用于Extras,除非已明确给予URI许可赠款(不便(,或者还通过添加ClipData
。