首页 > 解决方案 > FileProvider - java.lang.IllegalArgumentException:找不到配置的根目录,其中包含

问题描述

我正在尝试使用 FileProvider 公开 URI,但一直遇到此异常:

java.lang.IllegalArgumentException: Failed to find configured root that contains /data/data/com.example.android.development/cache/images/background.png

AndroidManifest.xml:

<provider
    android:name="androidx.core.content.FileProvider"
    android:authorities="${applicationId}.provider"
    android:exported="false"
    android:grantUriPermissions="true">
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/provider_paths"/>
</provider>

provider_paths.xml:

<paths>
    <cache-path name="images" path="images/"/>
</paths>

保存文件:

fun cache(context: Context, bitmap: Bitmap, fileName: String) {
    val filePath = File(context.cacheDir, "images")
    val file = File(filePath, fileName)
    try {
        filePath.mkdirs()
        val fos = FileOutputStream(file)
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos)
        fos.close()
    } catch (e: IOException) {
        throw RuntimeException(e)
    }
}

获取 URI:

fun getUri(context: Context, fileName: String): Uri {
    val filePath = File(context.cacheDir, "images")
    val file = File(filePath, fileName)
    return FileProvider.getUriForFile(context, "${context.applicationContext.packageName}.provider", file)
}

我做了一些调试,似乎该FileProvider getUriForFile(File file)函数首先将文件路径转换/data/user/0/com.example.android.development/cache/images/background.png为规范文件路径/data/data/com.example.android.development/cache/images/background.png,然后尝试将根路径/storage/emulated/0与规范文件路径匹配。当它没有找到这个匹配时,它会抛出异常。我不确定我做错了什么导致这个或如何解决这个问题?

调试 FileProvider.java getUriForFile(File file):

@Override
public Uri getUriForFile(File file) { // file: "/data/user/0/com.example.android.development/cache/images/background.png"
    String path; // path: "/data/data/com.example.android.development/cache/images/background.png"
    try {
        path = file.getCanonicalPath();
    } catch (IOException e) {
        throw new IllegalArgumentException("Failed to resolve canonical path for " + file);
    }

    // Find the most-specific root path
    Map.Entry<String, File> mostSpecific = null;
    for (Map.Entry<String, File> root : mRoots.entrySet()) {
        final String rootPath = root.getValue().getPath(); // rootPath: "/storage/emulated/0"
        if (path.startsWith(rootPath) && (mostSpecific == null // path: "/data/data/com.example.android.development/cache/images/background.png"
                || rootPath.length() > mostSpecific.getValue().getPath().length())) {
            mostSpecific = root; // mostSpecific: null
        }
    }

    if (mostSpecific == null) {
        throw new IllegalArgumentException(
                "Failed to find configured root that contains " + path);
    }

    // Start at first char of path under root
    final String rootPath = mostSpecific.getValue().getPath();
    if (rootPath.endsWith("/")) {
        path = path.substring(rootPath.length());
    } else {
        path = path.substring(rootPath.length() + 1);
    }

    // Encode the tag and path separately
    path = Uri.encode(mostSpecific.getKey()) + '/' + Uri.encode(path, "/");
    return new Uri.Builder().scheme("content")
            .authority(mAuthority).encodedPath(path).build();
}

我已经尝试过的事情:

  1. 添加<root-path name="root" path="." /><paths>
  2. 尝试filesDir代替cacheDir
  3. 在手机而不是模拟器上运行应用程序

标签: javaandroidkotlinandroidxandroid-fileprovider

解决方案


您的项目有多个名为 的资源res/xml/provider_paths.xml,并且您正在结束一个用于其他人的资源FileProvider

例如,您可能有一个app/模块和一个依赖于的something/库模块。app/如果他们都拥有res/xml/provider_paths.xml,那么 IIRC app/“获胜”,并且它的资源被包括在内。

最简单的解决方案是将您的资源重命名为其他名称(例如,res/xml/images_provider_paths.xml)并更新您的<provider>清单条目以指向新名称。


推荐阅读