使用Firemonkey / Delphi打开Android 26的PDF文件时获取异常 [英] Getting exception while opening PDF file for Android 26 using Firemonkey/Delphi

查看:120
本文介绍了使用Firemonkey / Delphi打开Android 26的PDF文件时获取异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Delphi 10.1 Berlin开发Android移动应用程序,我需要从该应用程序打开PDF文件,但出现错误- android.os.FileUriExposedException:file:/// Storage / emulated / 0 / Download / AppDataDetails / 1.Pdf通过Intent.getData()在应用程序之外公开,下面我提到了代码:

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.FileUriExposedException: file:///Storage/emulated/0/Download/AppDataDetails/1.Pdf exposed beyond app through Intent.getData() and below I have mentioned the code:

AndroidManifest.template .xml:

AndroidManifest.template.xml:

<!-- **** ADD THIS SECTION **** -->
<provider
    android:name="android.support.v4.content.FileProvider"
    android:authorities="com.AatchiyarIAS.fileprovider"
    android:exported="false"
    android:grantUriPermissions="true">
    <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/provider_paths"/>
</provider>

provider_paths.xml:

provider_paths.xml:

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
  <external-path name="external_files" path="."/>
</paths>

Delphi代码:

function TFrameFileDetail.GetMimeType(Uri: Jnet_Uri): JString;
var
  MimeType: JString;
  ContentResolver: JContentResolver;
  FileExtension: JString;
begin
  // https://stackoverflow.com/a/31691791/2899073

  MimeType := nil;
  if (Uri.getScheme.equals(TJContentResolver.JavaClass.SCHEME_CONTENT)) then
  begin
    ContentResolver := TAndroidHelper.Context.getContentResolver();
    MimeType := ContentResolver.getType(uri);
  end
  else
  begin
    FileExtension := TJMimeTypeMap.JavaClass.getFileExtensionFromUrl(uri.toString());

    MimeType := TJMimeTypeMap.JavaClass.getSingleton().getMimeTypeFromExtension(
      fileExtension.toLowerCase()
    );
  end;

  Result := MimeType;
end;


procedure TFrameFileDetail.OpenPDF(InpStrPDFPath: string);
var
  Data: Jnet_Uri;
  Intent: JIntent;
  javafile: JFile;
  &OriginalFile, PublicDirectoryFile, PublicFile: JFile;
  PublicDirectoryPath, PublicPath: string;
  Uri: Jnet_Uri;
begin
  PublicFile := TJFile.JavaClass.init(StringToJString(InpStrPDFPath));
  Intent := TJIntent.JavaClass.init(TJIntent.JavaClass.ACTION_VIEW);
  Uri := TJnet_Uri.JavaClass.fromFile(PublicFile);
  Intent.setDataAndType(Uri, self.GetMimeType(Uri));
  Intent.setFlags(TJIntent.JavaClass.FLAG_ACTIVITY_NO_HISTORY);
  TAndroidHelper.Activity.startActivity(Intent);
end;

请帮助我解决此问题。

Please help me to fix this issue.

推荐答案

您将需要此单元:

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

此代码:

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;

这篇关于使用Firemonkey / Delphi打开Android 26的PDF文件时获取异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆