排查‘failed to find configured root that contains’错误及Uri生成失败问题
生成Uri时触发"failed to find configured root that contains"错误的排查
生成Uri时遇到failed to find configured root that contains错误,自查拼写未发现问题,以下是相关代码、配置及尝试过的修改,请帮忙定位问题:
Delphi代码
var files: Tfile; lIntent: JIntent; lJRecipient: TJavaObjectArray<JString>; lURI: Jnet_Uri; LAuthority: JString; lJFile: JFile; lFileName: string; lFilePath: string; lFilePathFileName: string; Surf : TBitmapSurface; begin lFileName := Concat('Foto','Code' ,'.jpg') ; lFilePath := System.IOUtils.TPath.GetPublicPath; lFilePathFileName := System.IOUtils.Tpath.Combine(lFilePath, lFileName) ; Surf:=TBitMapSurface.Create; SaveParams.Quality:=100; try Surf.Assign(imResult.Bitmap); TbitMapCodecManager.SaveToFile(lFilePathFileName, Surf, @SaveParams); finally Surf.Free; end; lIntent:= TJIntent.Create; lIntent.setType(StringToJString('message/rfc822')); lIntent.setAction(TJIntent.JavaClass.ACTION_SEND); lIntent.setFlags(TJIntent.JavaClass.FLAG_ACTIVITY_NEW_TASK); lIntent.putExtra(TJIntent.JavaClass.EXTRA_EMAIL, lJRecipient); lIntent.putExtra(TJIntent.JavaClass.EXTRA_SUBJECT, StringToJString('Text')); lIntent.putExtra(TJIntent.JavaClass.EXTRA_TEXT, StringToJString('.....')); LAuthority := StringToJString(JStringToString( TAndroidHelper.Context.getApplicationContext.getPackageName)+'.fileprovider'); lJFile:= TJFile.JavaClass.init(StringToJString(lFileName)); lUri := TJFileProvider.JavaClass.getUriForFile( TAndroidHelper.Context, LAuthority, lJFile);
AndroidManifest配置
<provider android:name="android.support.v4.content.FileProvider" android:authorities="com.embarcadero.ScanDecoder.fileprovider" android:grantUriPermissions="true" android:exported="false"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/filepaths" /> </provider>
filepaths.xml内容
<?xml version="1.0" encoding="utf-8"?> <paths xmlns:android="http://schemas.android.com/apk/res/android"> <files-path name="MyFoto" path="."/> </paths>
尝试过的path修改
- path="."
- path="./"
- path="files/"
- path=".files/"
问题定位与修正方案
文件路径不匹配:
代码中使用System.IOUtils.TPath.GetPublicPath获取的是设备公共存储路径,但filepaths.xml里配置的<files-path>对应的是App内部私有存储的files目录,两者完全不对应,导致FileProvider找不到匹配的根目录。JFile初始化错误:
创建lJFile时仅传入了文件名lFileName,而非完整文件路径lFilePathFileName,这会让FileProvider无法定位到实际存储的文件位置。修正步骤:
- 调整
JFile初始化代码:lJFile:= TJFile.JavaClass.init(StringToJString(lFilePathFileName)); - 修改
filepaths.xml,匹配公共存储路径(若使用公共路径,改用<external-path>标签):<?xml version="1.0" encoding="utf-8"?> <paths xmlns:android="http://schemas.android.com/apk/res/android"> <external-path name="MyFoto" path="."/> </paths> - 确认Authority一致性:代码中动态生成的Authority是包名+
.fileprovider,需保证与Manifest中android:authorities的值完全一致,若包名不是com.embarcadero.ScanDecoder,需同步修改Manifest中的配置。
- 调整
内容的提问来源于stack exchange,提问作者Юрий
相关产品推荐
相关产品推荐

