[英]Xamarin FileProvider can't open files in Android
in my Xamarin Forms application I create pdfs that can be opened with Android application. 在我的Xamarin Forms应用程序中,我创建了可以用Android应用程序打开的pdf。
With device with Android 7 all works fine, now I have tested it with Android 8 and the file can't be open. 使用装有Android 7的设备,一切正常,现在我已经使用Android 8对其进行了测试,并且该文件无法打开。
I have installed the Xam.PluginMedia and leave all default settings: 我已经安装了Xam.PluginMedia并保留所有默认设置:
in the MainActivity I have override the OnRequestPermissionsResult function with: 在MainActivity中,我使用以下方法覆盖了OnRequestPermissionsResult函数:
Plugin.Permissions.PermissionsImplementation.Current.OnRequestPermissionsResult(requestCode, permissions, grantResults);
the file_paths.xaml is: file_paths.xaml是:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-files-path name="my_images" path="Pictures" />
<external-files-path name="my_movies" path="Movies" />
</paths>
and in the AndroidManifest.xml I have added: 并且在AndroidManifest.xml中添加了:
<provider android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths"></meta-data>
The code to open the preview is this: 打开预览的代码是这样的:
public void OpenPreviewFile(string FilePath)
{
Java.IO.File JavaFile = new Java.IO.File(FilePath);
Android.Net.Uri Path = FileProvider.GetUriForFile(global::Android.App.Application.Context, global::Android.App.Application.Context.PackageName + ".fileprovider", JavaFile);
string Extension = global::Android.Webkit.MimeTypeMap.GetFileExtensionFromUrl(Path.ToString());
string MimeType = global::Android.Webkit.MimeTypeMap.Singleton.GetMimeTypeFromExtension(Extension);
Intent Intent = new Intent(Intent.ActionView);
Intent.SetDataAndType(Path, MimeType);
Intent.AddFlags(ActivityFlags.GrantReadUriPermission);
Intent.SetFlags(ActivityFlags.ClearWhenTaskReset | ActivityFlags.NewTask);
try
{
MainActivity.AppInstance.StartActivity(Intent.CreateChooser(Intent, TS.Translate("ANDROID_ScegliApp")));
}
catch (System.Exception)
{
Toast.MakeText(MainActivity.AppInstance, TS.Translate("ANDROID_ImpossibileAprireIlFIle"), ToastLength.Short).Show();
}
}
The files generated are under this path: Android/data/MY_PACKAGE_ID/files/GeneratedReports
生成的文件在以下路径下:
Android/data/MY_PACKAGE_ID/files/GeneratedReports
I have attached a video to show the actual behavior. 我已附上视频以显示实际行为。
I found the problem. 我发现了问题。
Inside the file_paths.xml
, the "path" value of the nodes must be started with "/", 在
file_paths.xml
,节点的“路径”值必须以“ /”开头,
so with this node: <files-path name="GeneratedReports" path="/GeneratedReports" />
my code works. 因此,使用以下节点:
<files-path name="GeneratedReports" path="/GeneratedReports" />
我的代码有效。
Also I needed to remove MyIntent.SetFlags(ActivityFlags.ClearWhenTaskReset | ActivityFlags.NewTask);
另外我还需要删除
MyIntent.SetFlags(ActivityFlags.ClearWhenTaskReset | ActivityFlags.NewTask);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.