如何通过默认系统 pdf 应用程序中的 FileProvider 在 Xamarin.Android 中打开内部创建的 pdf? [英] How do I open an internally created pdf in Xamarin.Android via a FileProvider in the default system pdf app?

查看:19
本文介绍了如何通过默认系统 pdf 应用程序中的 FileProvider 在 Xamarin.Android 中打开内部创建的 pdf?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我通过的私有本地目录中创建了一个 .pdf 文件(我尝试使用最少的权限):

I created a .pdf file in the private local directory I got via (I try to work with the minimum of permissions):

string targetBaseDir = Environment.GetFolderPath(
    Environment.SpecialFolder.Personal,
    Environment.SpecialFolderOption.Create
    );

官方 Android 文档 建议该文件应通过 FileProvider 共享.

The official Android documentation suggests that file should be shared via a FileProvider.

我还尝试获取一些代码来启动一个意图,我目前在:

I also tried to get some code to start an intent, and I'm currently at:

var fileUri = Android.Net.Uri.Parse(filePath);

var intent = new Intent();
intent.SetFlags(ActivityFlags.ClearTop);
intent.SetFlags(ActivityFlags.NewTask); 
intent.SetAction(Intent.ActionView);
intent.SetType("application/pdf");
intent.PutExtra(Intent.ExtraStream, fileUri);
intent.AddFlags(ActivityFlags.GrantReadUriPermission);
Android.App.Application.Context.StartActivity(intent);

这会启动 pdf 文件的共享对话框,但当 Adob​​e Pdf 阅读器打开时,它显示一个空视图,而不是我的 pdf 文件.

This starts the share dialog for a pdf file but while the Adobe Pdf reader gets opened it shows an empty view and not my pdf file.

推荐答案

您需要使用 FileProvider 包装您的 URI.由于 android uri 会给你 file:// 而 FileProvider 会给你 content://,这是你真正需要的:

You need to wrap your URI with FileProvider. Since android uri will give you file:// while FileProvider will give you content://, which you actually need:

public static Android.Net.Uri WrapFileWithUri(Context context,Java.IO.File file)
{
    Android.Net.Uri result;
    if (Build.VERSION.SdkInt < (BuildVersionCodes)24)
    {
        result = Android.Net.Uri.FromFile(file);
    }
    else
    {
        result = FileProvider.GetUriForFile(context, context.ApplicationContext.PackageName + ".provider", file);
    }
    return result;
}

文件可以这样创建:

var file = new Java.IO.File(filePath); 

然后就可以打开了:

    public static void View(Context context, string path, string mimeType) 
    {
        Intent viewIntent = new Intent(Intent.ActionView);
        Java.IO.File document = new Java.IO.File(path);            
        viewIntent.SetDataAndType(UriResolver.WrapFileWithUri(context,document),mimeType);            
        viewIntent.SetFlags(ActivityFlags.NewTask);
        viewIntent.AddFlags(ActivityFlags.GrantReadUriPermission);
        context.StartActivity(Intent.CreateChooser(viewIntent, "your text"));
    }

请注意,这条线

viewIntent.SetDataAndType(UriResolver.WrapFileWithUri(context,document),mimeType);

不等于 SetData 和 SetType 分开命令.

does not equal to SetData and SetType separate commands.

是的,您需要将 FileProvider 添加到您的清单中:

And yes, you need to add FileProvider to your manifest:

<provider android:name="android.support.v4.content.FileProvider" android:authorities="${applicationId}.provider" android:exported="false" android:grantUriPermissions="true">
            <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/provider_paths" />
        </provider>

您还需要使用 provider_paths.xml 文件创建 Resources\xml 文件夹:

Also you need to create Resources\xml folder with provider_paths.xml file:

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

</paths>

这篇关于如何通过默认系统 pdf 应用程序中的 FileProvider 在 Xamarin.Android 中打开内部创建的 pdf?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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