You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何解决java.lang.IllegalArgumentException: Failed to find configured root异常?

解决FileProvider IllegalArgumentException: Failed to find configured root问题

问题原因

你的files.xml配置的是映射getFilesDir()下的images/目录,但代码中创建的文件路径是getFilesDir()/my_images/cool.jpg,两者路径不匹配,导致FileProvider找不到对应的配置根目录。

解决方案(二选一即可)

方案1:修改files.xml配置

将files-path的path属性改为my_images/,匹配代码中的目录结构:

<paths>
    <files-path name="my_images" path="my_images/"/>
</paths>

方案2:修改代码中的文件路径

将代码中的目录改为images,匹配files.xml的配置:

File testFile = new File(getContext().getFilesDir(), "images");
File testFile2 = new File(testFile, "cool.jpg");
Uri uri = FileProvider.getUriForFile(getContext(),
    "com.bignerdranch.android.criminalintent.fileprovider",
    testFile2);

补充说明

files-path的path参数指定的是应用内部存储files目录下的子目录,name参数是该目录在生成的Uri中的别名。确保代码中文件的实际路径和配置中的path指向的目录一致,才能让FileProvider正确映射生成Uri。

内容的提问来源于stack exchange,提问作者VanechikSpace

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 06:50:22