FileProvider"无法找到已配置的根"异常

Jen*_*eni 2 android android-fileprovider

还有一个这样的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>
Run Code Online (Sandbox Code Playgroud)

file_paths.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>
Run Code Online (Sandbox Code Playgroud)

创建附件:

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();
}
Run Code Online (Sandbox Code Playgroud)

和Uri的创作:

    File readF = new File(fullFileName);
Uri uri = FileProvider.getUriForFile(this, "todolistj.todolist.fileprovider", readF);
Run Code Online (Sandbox Code Playgroud)

fullFileName用于文件创建的代码段中返回的值在哪里.

在Uri创建行上,我得到例外:

...
Caused by: java.lang.IllegalArgumentException: Failed to find configured 
root that contains /storage/emulated/0/Android/data/todolistj.todolist/files/export/MyCache
...
Run Code Online (Sandbox Code Playgroud)

正如此处的文档(https://developer.android.com/reference/android/support/v4/content/FileProvider.html)所述:

<external-files-path name ="name"path ="path"/>表示应用程序外部存储区域根目录中的文件.此子目录的根路径与Context#getExternalFilesDir(String)Context.getExternalFilesDir(null)返回的值相同.

所以我的xml中的external-files-path似乎与context.getExternalFilesDir我正在使用的方法相匹配.这两个地方我都有"导出"文件夹.当局似乎匹配......可能是我的问题.我发现无法找到并打印FileProvider的"已配置的根"

Jen*_*eni 5

我似乎找到了解决方法或修复.改变我是从使用的根目录类型external-files-pathcache-path在XML和context.getExternalFilesDir("export");File dir = new File(context.getCacheDir(), "export");用于获取文件夹中创建文件.

我成功附加了该文件.请注意,在FileProvider类的FileProvider.java中,我找到了以下用于构造Uris的代码:

        if (TAG_ROOT_PATH.equals(tag)) {
            target = buildPath(DEVICE_ROOT, path);
        } else if (TAG_FILES_PATH.equals(tag)) {
            target = buildPath(context.getFilesDir(), path);
        } else if (TAG_CACHE_PATH.equals(tag)) {
            target = buildPath(context.getCacheDir(), path);
        } else if (TAG_EXTERNAL.equals(tag)) {
            target = buildPath(Environment.getExternalStorageDirectory(), path);
        }
Run Code Online (Sandbox Code Playgroud)

看起来只支持以下4个文件夹:TAG_ROOT_PATH,TAG_FILES_PATH,TAG_CACHE_PATH,TAG_EXTERNAL.没有TAG_EXTERNAL_FILES或类似的东西,所以这似乎是文档和实现之间的不匹配,实际上不支持external-files-path匹配getExternalFilesDir方法.