Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.1k views
in Technique[技术] by (71.8m points)

delphi - android.os.FileUriExposedException: <filename> exposed beyond app through Intent.getData()

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)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
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;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...