如何在Xamarin android c#应用程序中显示移动存储中的PDF?

问题描述 投票:0回答:1

我正在使用C#在xmarin android应用程序中工作。我想打开内部存储“ TESTFOLDER”的自定义文件夹中的pdf文件。每次转到要启动活动的catch部分时,下面都会给出代码。怎么打开呢?enter image description here

代码

var externalPath = global::Android.OS.Environment.ExternalStorageDirectory.Path + "/TESTFOLDER";
                Java.IO.File file = new Java.IO.File(externalPath, "qdummy.pdf");
                if(file.Exists())
                {
                    try
                    {
                        file.SetReadable(true);
                        Android.Net.Uri uri = Android.Net.Uri.FromFile(file);
                        Intent intent = new Intent(Intent.ActionView);
                        intent.SetDataAndType(uri, "application/pdf");
                        intent.SetFlags(ActivityFlags.ClearWhenTaskReset | ActivityFlags.NewTask);                        
                        intent.SetFlags(ActivityFlags.NoHistory);
                        StartActivity(intent);                        
                    }
                    catch (Exception ex)
                    {
                        Toast.MakeText(Application.Context, "problem", ToastLength.Long).Show();
                    }
                }
                else
                {
                    Toast.MakeText(Application.Context, "No exx", ToastLength.Long).Show();
                }
c# android pdf xamarin start-activity
1个回答
0
投票

根据您的描述和错误消息,您需要使用FileProvider包裹URI。由于android uri会给您file://,而FileProvider会首先给您content://。

 Android.Net.Uri pdfPath = FileProvider.GetUriForFile(context, context.ApplicationContext.PackageName + ".provider", file);

然后您需要将FileProvider添加到清单中:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.companyname.pdfsample" android:installLocation="auto">
<uses-sdk android:minSdkVersion="21" android:targetSdkVersion="28" />
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme">
    <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/file_paths" />
    </provider>
</application>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
</manifest>

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

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

打开pdf代码:

 var externalPath = global::Android.OS.Environment.ExternalStorageDirectory.Path + "/testfolder";
        Java.IO.File file = new Java.IO.File(externalPath, "test.pdf");
        if(file.Exists())
        {
            var context = Android.App.Application.Context;
            try
            {

                Android.Net.Uri pdfPath = FileProvider.GetUriForFile(context, context.ApplicationContext.PackageName + ".provider", file);
                context.GrantUriPermission(context.PackageName, pdfPath, ActivityFlags.GrantReadUriPermission);
                Intent intent = new Intent();
                intent.SetFlags(ActivityFlags.GrantReadUriPermission);
                intent.SetAction(Android.Content.Intent.ActionView);
                intent.SetFlags(ActivityFlags.NoHistory);
                intent.SetFlags(ActivityFlags.ClearWhenTaskReset | ActivityFlags.NewTask);
                intent.SetDataAndType(pdfPath, "application/pdf");
                context.StartActivity(intent);

            }
            catch (Exception ex)
            {
                Toast.MakeText(Application.Context, "problem", ToastLength.Long).Show();
            }

        }

这里是您可以查看的相同线程:

How do I open an internally created pdf in Xamarin.Android via a FileProvider in the default system pdf app?

© www.soinside.com 2019 - 2024. All rights reserved.