gpt4 book ai didi

android - 无法使用 FileProvider 以编程方式将文件附加到电子邮件 Intent

转载 作者:行者123 更新时间:2023-12-03 17:29:17 24 4
gpt4 key购买 nike

我正在创建电子邮件 Intent 并附加文件(使用 FileProvider),但 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。第一个曾经在 FileProviders 成为强制性之前工作。第二个失败的方式与上面的代码相同。

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: Null AssetFileDescriptor while calling openAssetFileDescriptor.

[ list 截图]
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.pentad.bridge.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_provider_paths"/>
</provider>

[文件 file_provider_paths.xml]
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="app_root" path="."/>
</paths>

最佳答案

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

yourIntent.clipData = ClipData.newUri(context.contentResolver, fileName, contentUri)
罪魁祸首是 Intent.FLAG_GRANT_READ_URI_PERMISSION仅适用于 Intent.dataIntent.clipData ,而不是额外的,除非已明确授予 uri 权限授予(不方便),或者已通过添加 ClipData 来授予.

关于android - 无法使用 FileProvider 以编程方式将文件附加到电子邮件 Intent ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57679029/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com