Getting exception while opening PDF file for Android 26 using Firemonkey/Delphi

后端 未结 1 1904
情话喂你
情话喂你 2021-01-20 23:30

I\'m using Delphi 10.1 Berlin for developing Android mobile application and I need to open the PDF file from the application but I\'m getting the error - android.os.File

相关标签:
1条回答
  • 2021-01-21 00:08

    You'll need this unit:

    https://github.com/DelphiWorlds/KastriFree/blob/master/API/DW.Androidapi.JNI.FileProvider.pas

    for this code:

    uses
      Androidapi.JNI.JavaTypes, Androidapi.JNI.Net, Androidapi.JNI.GraphicsContentViewText, Androidapi.Helpers;
    
    procedure OpenPDF(const AFileName: string);
    var
      LIntent: JIntent;
      LAuthority: JString;
      LUri: Jnet_Uri;
    begin
      LAuthority := StringToJString(JStringToString(TAndroidHelper.Context.getApplicationContext.getPackageName) + '.fileprovider');
      LUri := TJFileProvider.JavaClass.getUriForFile(TAndroidHelper.Context, LAuthority, TJFile.JavaClass.init(StringToJString(AFileName)));
      LIntent := TJIntent.JavaClass.init(TJIntent.JavaClass.ACTION_VIEW);
      LIntent.setDataAndType(LUri, StringToJString('application/pdf'));
      LIntent.setFlags(TJIntent.JavaClass.FLAG_GRANT_READ_URI_PERMISSION);
      TAndroidHelper.Activity.startActivity(LIntent);
    end;
    
    0 讨论(0)
提交回复
热议问题