【发布时间】:2018-10-18 08:23:22
【问题描述】:
我正在尝试使用 FileProvider 内容提供程序共享我的内部日志文件。我在清单中有以下<provider> 条目:
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="nl.charm.nedradio"
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>
<external-files-path name="files" path="." />
</paths>
创建分享意图的代码是:
private static final String LOGFILE_NAME = "log.txt";
private static final String AUTHORITY = "nl.charm.nedradio";
public static Intent getShareIntent(Context context)
{
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_SUBJECT, "Log File");
intent.putExtra(Intent.EXTRA_TEXT, "App logfile.");
// Allow access outside of share application's realm
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
File logFile = new File(context.getExternalFilesDir(null), LOGFILE_NAME);
Uri logURI = FileProvider.getUriForFile(context, AUTHORITY, logFile);
intent.putExtra(Intent.EXTRA_STREAM, logURI);
return intent;
}
意图的创建正在正常工作,但是当我尝试与例如分享时Gmail 我在 logcat 中收到以下错误:
2018-10-18 10:16:49.536 4585-4585/com.google.android.gm E/Gmail: Gmail:Error adding attachment
exk: FileNotFoundException when openFileDescriptor.
我已经搜索了答案,但找不到。那么关于我做错了什么有什么建议吗?
【问题讨论】:
-
您可以尝试以下更改您的“file_provider_paths.xml”并让我知道更新:
<?xml version="1.0" encoding="utf-8"?> <paths> <external-files-path name="log" path="." /> </paths> -
我刚刚提供了一个解决问题的答案。
标签: android android-fileprovider