且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

android.os.FileUriExposedException:< filename>通过Intent.getData()在应用程序之外公开

更新时间:2022-11-16 17:34:41

 使用
Androidapi.JNI.GraphicsContentViewText,
Androidapi.JNI.Media,
Androidapi.Helpers,
Androidapi.JNI.Net,
Androidapi.JNI.JavaTypes,
Androidapi.JNI.App,
Androidapi.JNI.Os,
Androidapi.JNI.Support,
System.IOUtils;

过程TTabbedForm.Button1Click(Sender:TObject);
var
Intent:JIntent;
FileName,DestFileName:字符串;
数据:Jnet_Uri;
CompName:JComponentName;
lFile:JFile;
const
IMAGE_FILENAME =‘small_what.jpg’;
开始

FileName:= System.IOUtils.TPath.GetPublicPath + PathDelim + IMAGE_FILENAME; //部署到资产

DestFileName:= TPath.GetDownloadsPath + PathDelim + IMAGE_FILENAME;
TFile.Copy(FileName,DestFileName,true);

Intent:= TJIntent.Create;
Intent.setAction(TJIntent.JavaClass.ACTION_VIEW);
如果TJBuild_VERSION.JavaClass.SDK_INT> = TJBuild_VERSION_CODES.JavaClass.N,则
开始
lFile:= TJFile.JavaClass.init(StringToJString(FileName));
Intent.setFlags(TJIntent.JavaClass.FLAG_GRANT_READ_URI_PERMISSION);
数据:= TJFileProvider.JavaClass.getUriForFile(TAndroidHelper.Context,
StringToJString('com.embarcadero.TestIntents.fileprovider'),lFile);
结束
否则
数据:= TJnet_Uri.JavaClass.parse(StringToJString(’file://’+ DestFileName));

Intent.setDataAndType(Data,StringToJString(’image / jpg’));

试试
TAndroidHelper.Activity.startActivity(Intent);
,但E上的
除外:
开始
Label1.Text:= E.Message;
结尾;
结尾;


In earlier versions of Delphi (prior to 10.3 Rio), which targeted Android API < 24, it was possible to create file intents as shown in the answer to question opening the image with the external gallery using delphi

However, now that 10.3 targets Android API >= 24, that code, produces the error that is the subject of this question.

I endeavoured to answer the question at Delphi Use android fileprovider to send intent to open and image file with the default android gallery but that question was closed as a duplicate even though the answer the closer linked to, is in Android Java and not Delphi. My answer is below (which follows after a few protracted hours of research)

uses
  Androidapi.JNI.GraphicsContentViewText,
  Androidapi.JNI.Media,
  Androidapi.Helpers,
  Androidapi.JNI.Net,
  Androidapi.JNI.JavaTypes,
  Androidapi.JNI.App,
  Androidapi.JNI.Os,
  Androidapi.JNI.Support,
  System.IOUtils;

procedure TTabbedForm.Button1Click(Sender: TObject);
var
  Intent: JIntent;
  FileName, DestFileName: string;
  Data: Jnet_Uri;
  CompName: JComponentName;
  lFile: JFile;
const
  IMAGE_FILENAME = 'small_what.jpg';
begin

  FileName := System.IOUtils.TPath.GetPublicPath + PathDelim + IMAGE_FILENAME; // deployed to "assets"

  DestFileName := TPath.GetDownloadsPath + PathDelim + IMAGE_FILENAME;
  TFile.Copy(FileName, DestFileName, true);

  Intent := TJIntent.Create;
  Intent.setAction(TJIntent.JavaClass.ACTION_VIEW);
  if TJBuild_VERSION.JavaClass.SDK_INT >= TJBuild_VERSION_CODES.JavaClass.N then
  begin
    lFile := TJFile.JavaClass.init(StringToJString(FileName));
    Intent.setFlags(TJIntent.JavaClass.FLAG_GRANT_READ_URI_PERMISSION);
    Data := TJFileProvider.JavaClass.getUriForFile(TAndroidHelper.Context,
      StringToJString('com.embarcadero.TestIntents.fileprovider'), lFile);
  end
  else
    Data := TJnet_Uri.JavaClass.parse(StringToJString('file://' + DestFileName));

  Intent.setDataAndType(Data, StringToJString('image/jpg'));

  try
    TAndroidHelper.Activity.startActivity(Intent);
  except
    on E: Exception do
    begin
      Label1.Text := E.Message;
    end;
  end;