“failed to find configured root that contains”错误的原因是什么?

huangapple go评论96阅读模式
英文:

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&lt;JString&gt;;
lURI: Jnet_Uri;
LAuthority: JString;
lJFile: JFile;
lFileName: string;
lFilePath: string;
lFilePathFileName: string;
Surf : TBitmapSurface;
begin
 lFileName := Concat(&#39;Foto&#39;,&#39;Code&#39; ,&#39;.jpg&#39;) ;
 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(&#39;message/rfc822&#39;));
  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(&#39;Text&#39;));
  lIntent.putExtra(TJIntent.JavaClass.EXTRA_TEXT, StringToJString(&#39;.....&#39;));
  LAuthority := StringToJString(JStringToString(
  TAndroidHelper.Context.getApplicationContext.getPackageName)+&#39;.fileprovider&#39;);
  lJFile:= TJFile.JavaClass.init(StringToJString(lFileName));
  lUri := TJFileProvider.JavaClass.getUriForFile(
    TAndroidHelper.Context,  LAuthority, lJFile);

I have included in the manifest file

&lt;provider
                android:name=&quot;android.support.v4.content.FileProvider&quot;
                android:authorities=&quot;com.embarcadero.ScanDecoder.fileprovider&quot;
                android:grantUriPermissions=&quot;true&quot;
                android:exported=&quot;false&quot;&gt;
                &lt;meta-data
                    android:name=&quot;android.support.FILE_PROVIDER_PATHS&quot;
                    android:resource=&quot;@xml/filepaths&quot; /&gt;
            &lt;/provider&gt;

Created a file \res\xml\filepaths.xml

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;paths xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;&gt;
&lt;files-path name=&quot;MyFoto&quot; path=&quot;.&quot;/&gt;
&lt;/paths&gt;  

I tried changing the path in the file to the following. I am still getting this error.

path=&quot;.&quot;
path=&quot;./&quot;
path=&quot;files/&quot;
path=&quot;.files/&quot;

Where have I gone wrong?

答案1

得分: 1

@Dave Nottage建议的正确方法是使用FileProvider一起工作。
当您在项目选项的权限列表中启用“安全文件共享”选项时,xml清单和提供程序路径会自动生成。
现在它可以工作。
我提供一个有效的解决方案。

procedure TestSendEmail();
{$IF DEFINED(ANDROID)}
 var
  lIntent: JIntent;
  lJRecipient: TJavaObjectArray&lt;JString&gt;;
  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&lt;JString&gt;.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&lt;JString&gt;;
  lURI: Jnet_Uri;
  lFileName: string;
  lFilePath: string;
  lFilePathFileName: string;

  Delim :Char;
//  Surf : TBitmapSurface;
{$ENDIF}


begin
 lFileName := Concat(&#39;MyFile&#39; ,&#39;.jpg&#39;) ;
 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&lt;JString&gt;.Create(1);
  lJRecipient.Items[0]:= StringToJString(&#39;xxxxxxx@yyyy.com&#39;);

  lIntent:= TJIntent.Create;

  lIntent.setType(StringToJString(&#39;message/rfc822&#39;));
  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(&#39;Subj&#39;));
  lIntent.putExtra(TJIntent.JavaClass.EXTRA_TEXT, StringToJString(&#39;Text&#39;));

  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;

huangapple
  • 本文由 发表于 2023年5月29日 21:10:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76357675.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定