英文:
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("tmp_image_file_", ".jpg", cacheDir).apply {
deleteOnExit()
}
return FileProvider.getUriForFile(
context,
"${BuildConfig.APPLICATION_ID}.fileprovider",
tmpFile
)
}
My file_provider_paths.xml
looks like this:
<paths>,
<external-files-path
name="app_images"
path="." />
<cache-path
name="cached_files"
path="." />
<files-path
name="images"
path="." />
</paths>
In my Manifest I have:
<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>
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!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论