Android FileProvider.getUriForFile Failed to find configured root

huangapple go评论65阅读模式
英文:

Android FileProvider.getUriForFile Failed to find configured root

问题

以下是您提供的内容的翻译:

问题出现在我调用 getTmpFileUri() 时,我会得到类似以下的错误:

java.lang.IllegalArgumentException: 未能找到包含 /data/data/com.sample.myapp/cache/tmp_image_file_1764639852023756356.jpg 的已配置根目录

这是获取 Uri 的代码:

fun getTmpFileUri(context: Context, cacheDir: File): Uri {
    val tmpFile = File.createTempFile("tmp_image_file_", ".jpg", cacheDir).apply {
        deleteOnExit()
    }
    return FileProvider.getUriForFile(
        context,
        "${BuildConfig.APPLICATION_ID}.fileprovider",
        tmpFile
    )
}

我的 file_provider_paths.xml 如下:

<paths>,
    <external-files-path
        name="app_images"
        path="." />
    <cache-path
        name="cached_files"
        path="." />
    <files-path
        name="images"
        path="." />
</paths>

在我的清单文件中,我有以下内容:

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

我在两个其他应用中也使用完全相同的设置和代码,它们也使用相同的 minSdk、gradle 版本和库(33)。但出于某种原因,这个应用中的代码不起作用。这个应用更复杂,其中定义了许多权限。我已验证相机权限不在合并的 XML 文件中,以防会引发问题。

我尝试了各种 Stack Overflow 上的解决方案,其中一些建议更改 XML 文件,但对我来说并没有奏效。这个帖子与我的问题最接近,但其解决方案不起作用:https://stackoverflow.com/questions/61386649/android-fileprovider-geturiforfile-failed-to-find-configured-root

提前感谢您的任何建议!

英文:

The issue is when I call getTmpFileUri() I get something like this:

java.lang.IllegalArgumentException: Failed to find configured root that contains /data/data/com.sample.myapp/cache/tmp_image_file_1764639852023756356.jpg

Here is my code for getting the Uri:

fun getTmpFileUri(context: Context, cacheDir: File): Uri {
            val tmpFile = File.createTempFile(&quot;tmp_image_file_&quot;, &quot;.jpg&quot;, cacheDir).apply {
                deleteOnExit()
            }
            return FileProvider.getUriForFile(
                context,
                &quot;${BuildConfig.APPLICATION_ID}.fileprovider&quot;,
                tmpFile
            )
        }

My file_provider_paths.xml looks like this:

&lt;paths&gt;,
    &lt;external-files-path
        name=&quot;app_images&quot;
        path=&quot;.&quot; /&gt;
    &lt;cache-path
        name=&quot;cached_files&quot;
        path=&quot;.&quot; /&gt;
    &lt;files-path
        name=&quot;images&quot;
        path=&quot;.&quot; /&gt;
&lt;/paths&gt;

In my Manifest I have:

        &lt;provider
            android:name=&quot;androidx.core.content.FileProvider&quot;
            android:authorities=&quot;${applicationId}.fileprovider&quot;
            android:exported=&quot;false&quot;
            android:grantUriPermissions=&quot;true&quot;&gt;
            &lt;meta-data
                android:name=&quot;android.support.FILE_PROVIDER_PATHS&quot;
                android:resource=&quot;@xml/file_provider_paths&quot; /&gt;
        &lt;/provider&gt;

I am using the exact same setup and code in two other apps that also use the camera. The apps all use the same minSdk, gradle version, and libraries (33)For some reason the code in this app doesn't work. This app is more complicated and has a lot of permissions defined in it. I did verify that the camera permission is not in the merged xml file, in case that would cause an issue.

I've tried various SO solutions, some of which suggest changes to the xml file, which did not work for me. This post is the closest to my issue, but the solution does not work: <https://stackoverflow.com/questions/61386649/android-fileprovider-geturiforfile-failed-to-find-configured-root>

Thanks in advance for any suggestions!

答案1

得分: 0

谢谢你的快速回应。在这方面浪费了各种时间后,我没能找到任何问题,所以最终使缓存无效并重新启动(重新启动不够),然后哇,它起作用了!哦我的天,谢谢Android!

英文:

Thanks for quick responses. After wasting all kinds of time on this, I couldn't see any problems with it, so I finally Invalidated Cache and restarted (restart wasn't enough) and voila, it works! omg, thanks Android!

huangapple
  • 本文由 发表于 2023年3月21日 01:56:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/75793732.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定