我正在使用Delphi 10.1 Berlin开发Android移动应用程序,我需要从应用程序中打开PDF文件,但是我遇到了错误 - android.os.FileUriExposedException: file:///Storage/emulated/0/Download/AppDataDetails/1.Pdf exposed beyond app through Intent.getData(),下面是我的代码:
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:
<?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://dev59.com/iGoy5IYBdhLWcg3wZdCr#31691791
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;
请帮我解决这个问题。