这些 FileProvider 又一个失去了灵魂......我已经在这个问题上工作了一天多了,看来我错过了一些大东西。任何帮助,将不胜感激!
我正在尝试使用 FileProvider 发送带有附件的电子邮件。
我的 AndroidManifest.xml 的部分:
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="todolistj.todolist.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
文件路径.xml:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-files-path name="lists_to_send" path="export/"/>
</paths>
创建附件:
String content = "hello world";
File file;
FileOutputStream outputStream;
try {
File dir = context.getExternalFilesDir("export");
if(!dir.exists()) dir.mkdir();
file = new File(dir, "MyCache");
if (file.exists ()) file.delete ();
outputStream = new FileOutputStream(file);
outputStream.write(content.getBytes());
outputStream.close();
return file.getAbsolutePath();
} catch (IOException e) {
e.printStackTrace();
}
以及 Uri 的创建:
File readF = new File(fullFileName);
Uri uri = FileProvider.getUriForFile(this, "todolistj.todolist.fileprovider", readF);
where fullFileName
是创建文件的代码片段中返回的值。
在 Uri 创建行我遇到异常:
...
Caused by: java.lang.IllegalArgumentException: Failed to find configured
root that contains /storage/emulated/0/Android/data/todolistj.todolist/files/export/MyCache
...
正如这里的文档(https://developer.android.com/reference/android/support/v4/content/FileProvider.html https://developer.android.com/reference/android/support/v4/content/FileProvider.html) 状态:
表示应用程序外部存储区域根目录中的文件。该子目录的根路径与返回值相同
通过 Context#getExternalFilesDir(String)
Context.getExternalFilesDir(null)。
所以我的 xml 中的外部文件路径似乎与context.getExternalFilesDir
我正在使用的方法。
这两个地方都有“导出”文件夹。当局似乎很匹配......我的问题可能是什么。我发现无法找到并打印 FileProvider 的“配置根目录”