如何解决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
相关产品推荐
相关产品推荐

