简体   繁体   English

Xamarin FileProvider无法在Android中打开文件

[英]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. 我已附上视频以显示实际行为。

Video 视频

Here a sample 是一个样本

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.

相关问题 Android.Xamarin中打开的文件过多 - Too many open files in Android.Xamarin FileProvider.GetUriForFile在Xamarin-Android中与空引用异常崩溃 - FileProvider.GetUriForFile crashes with null reference exception in Xamarin-Android Xamarin Android AndroidX.Core.Content.FileProvider.GetUriForFile NullReferenceException - Xamarin Android AndroidX.Core.Content.FileProvider.GetUriForFile NullReferenceException Xamarin.Android(Visual 2019)-将released.apk视为存档-无法打开/安装 - Xamarin.Android (Visual 2019) - sees released .apk as an archive - can't open/install 在 Android 的新 Intent 中使用 FileProvider 在公共存储中打开 .DocX 文件时出错 - Error opening .DocX files in Public storage with FileProvider in new Intent in Android 无法在Xamarin Android上按数组设置TextColor - Can't SetTextColor by Array on Xamarin Android Xamarin Studio:无法签名和分发Xamarin Forms Android应用程序 - Xamarin Studio: Can't Sign and Distribute Xamarin Forms Android App 无法运行或访问 xamarin xamarin.Forms.Platform.Android - Can't run or access xamarin xamarin.Forms.Platform.Android 无法播放此视频Xamarin.Android - Can't play this video Xamarin.Android 无法在视图android / xamarin中滚动 - Can't scroll in view android/xamarin
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM