delphi android打开外部文件,报错android.os.FileUriExposedException解决方法

Android 7.0强制启用了被称作 StrictMode的策略,带来的影响就是你的App对外无法暴露file://类型的URI了。

如果你使用Intent 携带这样的URI 去打开外部App(比如:打开系统相机拍照),那么会抛出FileUriExposedException异常。

Delphi 为Android项目提供了Secure File Sharing选择项,默认是False。这一项是设置什么呢?

原来,Android 7及以后的版本,为了加强OS的安全性,不允许一个app访问其他app的文件,为了解决这个问题,将Secure File Sharing设置为True

完整代码如下:

Delphi 复制代码
unit Unit1;

interface

uses
   System.SysUtils, System.Types, System.UITypes, System.Classes,
   System.Variants, FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics,
   FMX.Dialogs, FMX.Controls.Presentation,
   {$IFDEF ANDROID}
   Androidapi.JNI.Support, Androidapi.JNI.GraphicsContentViewText,
   Androidapi.JNI.Net, Androidapi.JNI.Os, Androidapi.JNI.JavaTypes,
   Androidapi.Helpers,
   {$ENDIF}
   FMX.StdCtrls;

type
   TForm1 = class(TForm)
      Button1: TButton;
      procedure Button1Click(Sender: TObject);
   private
      { Private declarations }
   public
      { Public declarations }
   end;

var
   Form1: TForm1;

implementation

{$R *.fmx}

procedure TForm1.Button1Click(Sender: TObject);
var
   aUrl: string;
   {$IFDEF ANDROID}
   Intent: JIntent;
   Data: Jnet_Uri;
   {$ENDIF}
begin
   var path: string := '/sdcard/test.jpg';
   if FileExists(path) then
   begin
      {$IFDEF ANDROID}
      Intent := TJIntent.Create;
      Intent.setAction(TJIntent.JavaClass.ACTION_VIEW);
      if TJBuild_VERSION.JavaClass.SDK_INT >= TJBuild_VERSION_CODES.JavaClass.N then
      begin
         var lFile: JFile := TJFile.JavaClass.init(StringToJString(path));
         Intent.setFlags(TJIntent.JavaClass.FLAG_GRANT_READ_URI_PERMISSION);
         Data := TJcontent_FileProvider.JavaClass.getUriForFile(TAndroidHelper.Context, StringToJString('com.junwei.JWMeeting.fileprovider'), lFile);  //将这个com.junwei.JWMeeting 换成您的程序包
      end
      else
         Data := TJnet_Uri.JavaClass.parse(StringToJString('file://' + path));

      Intent.setDataAndType(Data, StringToJString('image/*'));
      try
         TAndroidHelper.Activity.startActivity(Intent);
      except
         on E: Exception do
         begin
            ShowMessage(E.Message)
         end;
      end;
      {$ENDIF ANDROID}
   end
   else
   begin
      ShowMessage('不存在文件:' + path)
   end;

end;

end.

后记:如果你的项目是用旧版delphi建的,如10.2,那么,需要在10.3.1下重建这个项目,才能确保Secure File Sharing选择项生效,生成正确的配置文件,不然,不会生成配置文件,有朋友遇到过。

相关推荐
Digitally2 小时前
如何用5种实用方法将电脑上的音乐传输到安卓手机
android·智能手机·电脑
HahaGiver6663 小时前
Unity与Android原生交互开发入门篇 - 打开Unity游戏的设置
android·unity·交互
2501_915909063 小时前
WebView 调试工具全解析,解决“看不见的移动端问题”
android·ios·小程序·https·uni-app·iphone·webview
IT乐手4 小时前
android 下载管理工具类
android
2501_915106325 小时前
App 怎么上架 iOS?从准备资料到开心上架(Appuploader)免 Mac 上传的完整实战流程指南
android·macos·ios·小程序·uni-app·iphone·webview
科技峰行者6 小时前
安卓16提前发布能否改写移动生态格局
android
蒲公英少年带我飞6 小时前
Android NDK 编译 protobuf
android
沐怡旸7 小时前
【底层机制】ART虚拟机深度解析:Android运行时的架构革命
android·面试
小禾青青7 小时前
uniapp安卓打包遇到报错:Uncaught SyntaxError: Invalid regular expression: /[\p{L}\p{N}]/
android·uni-app
studyForMokey8 小时前
【Kotlin内联函数】
android·开发语言·kotlin