英文:
How can I find the cause of the error: "failed to find configured root that contains"
问题
我有一个错误,但找不到它的原因。生成Uri时出错。
我查看了几个关于这个主题的帖子,这里讨论过。但是,错误无法纠正。我认为某处可能有拼写错误。我该如何找到我错在哪里?
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);
我已在清单文件中包含了以下内容
<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>
创建了一个文件 \res\xml\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="files/"
path=".files/"
我哪里错了?
英文:
I have an error and I can't find the cause of it. Error while generating Uri.
I looked through several topics on this topic, which were discussed here. However, the error could not be corrected. I think there is a typo somewhere. How can I find where I am wrong?
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);
I have included in the manifest file
<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>
Created a file \res\xml\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>
I tried changing the path in the file to the following. I am still getting this error.
path="."
path="./"
path="files/"
path=".files/"
Where have I gone wrong?
答案1
得分: 1
@Dave Nottage建议的正确方法是使用FileProvider一起工作。
当您在项目选项的权限列表中启用“安全文件共享”选项时,xml清单和提供程序路径会自动生成。
现在它可以工作。
我提供一个有效的解决方案。
procedure TestSendEmail();
{$IF DEFINED(ANDROID)}
var
lIntent: JIntent;
lJRecipient: TJavaObjectArray<JString>;
lURI: Jnet_Uri;
lFileName: string;
lFilePath: string;
lFilePathFileName: string;
Delim :Char;
// Surf : TBitmapSurface;
{$ENDIF}
begin
lFileName := Concat('MyFile' ,'.jpg') ;
lFilePath := System.IOUtils.TPath.GetPublicPath ;
lFilePathFileName := System.IOUtils.Tpath.Combine(lFilePath, lFileName) ;
Surf:=TBitMapSurface.Create;
SaveParams.Quality:=100;
try
Surf.Assign(imResult.Bitmap);
// Save file
TbitMapCodecManager.SaveToFile(lFilePathFileName, Surf, @SaveParams);
finally
Surf.Free;
end;
{$IF DEFINED(ANDROID)}
lJRecipient:= TJavaObjectArray<JString>.Create(1);
lJRecipient.Items[0]:= StringToJString('xxxxxxx@yyyy.com');
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('Subj'));
lIntent.putExtra(TJIntent.JavaClass.EXTRA_TEXT, StringToJString('Text'));
lUri:= TAndroidHelper.JFileToJURI(
TJFile.JavaClass.init(StringToJString(lFilePathFileName)));
lIntent.putExtra(TJIntent.JavaClass.EXTRA_STREAM,TJParcelable.Wrap(lUri)) ;
try
TAndroidHelper.Activity.startActivity(lIntent);
except
on E: Exception do
begin
ShowMessage( E.Message);
end;
end;
{$ENDIF}
end;
这是您提供的代码的翻译部分。
英文:
@Dave Nottage suggested the correct way to work with FileProvider.
The xml manifest and provider paths are generated automatically when you enable the "Secure File Sharing" option in the list of rights in the project options.
Now it works.
I provide a working solution.
procedure TestSendEmail();
{$IF DEFINED(ANDROID)}
var
lIntent: JIntent;
lJRecipient: TJavaObjectArray<JString>;
lURI: Jnet_Uri;
lFileName: string;
lFilePath: string;
lFilePathFileName: string;
Delim :Char;
// Surf : TBitmapSurface;
{$ENDIF}
begin
lFileName := Concat('MyFile' ,'.jpg') ;
lFilePath := System.IOUtils.TPath.GetPublicPath ;
lFilePathFileName := System.IOUtils.Tpath.Combine(lFilePath, lFileName) ;
Surf:=TBitMapSurface.Create;
SaveParams.Quality:=100;
try
Surf.Assign(imResult.Bitmap);
// Save file
TbitMapCodecManager.SaveToFile(lFilePathFileName, Surf, @SaveParams);
finally
Surf.Free;
end;
{$IF DEFINED(ANDROID)}
lJRecipient:= TJavaObjectArray<JString>.Create(1);
lJRecipient.Items[0]:= StringToJString('xxxxxxx@yyyy.com');
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('Subj'));
lIntent.putExtra(TJIntent.JavaClass.EXTRA_TEXT, StringToJString('Text'));
lUri:= TAndroidHelper.JFileToJURI(
TJFile.JavaClass.init(StringToJString(lFilePathFileName)));
lIntent.putExtra(TJIntent.JavaClass.EXTRA_STREAM,TJParcelable.Wrap(lUri)) ;
try
TAndroidHelper.Activity.startActivity(lIntent);
except
on E: Exception do
begin
ShowMessage( E.Message);
end;
end;
{$ENDIF}
end;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论