如何在 xamarin android c# 应用程序中显示移动存储中的 PDF?
how to show PDF from mobile storage in xamarin android c# app?
我正在使用 c# 编写 xmarin android 应用程序。我想打开一个位于内部存储自定义文件夹中的 pdf 文件 "TESTFOLDER"。下面给出了代码,每次它进入 catch 部分时,它都会启动 activity。如何打开它?
代码
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();
}
根据您的描述和错误信息,您需要使用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();
}
}
这是您可以查看的同一个主题:
问题已解决,
** 请按照本博客中所写的所有步骤进行操作,只需替换 try catch 块即可解决问题。
try
{
string extension = MimeTypeMap.GetFileExtensionFromUrl(Android.Net.Uri.FromFile(file).ToString());
string mimeType = MimeTypeMap.Singleton.GetMimeTypeFromExtension(extension);
Intent intent = new Intent(Intent.ActionView);
intent.SetFlags(ActivityFlags.ClearTop | ActivityFlags.NewTask);
Android.Net.Uri path = FileProvider.GetUriForFile(Application.Context, Android.App.Application.Context.PackageName + ".provider", file);
intent.SetDataAndType(path, mimeType);
intent.AddFlags(ActivityFlags.GrantReadUriPermission);
Application.Context.StartActivity(Intent.CreateChooser(intent, "Choose App"));
}
catch (Exception ex)
{
Toast.MakeText(Application.Context, "problem", ToastLength.Long).Show();
}
我正在使用 c# 编写 xmarin android 应用程序。我想打开一个位于内部存储自定义文件夹中的 pdf 文件 "TESTFOLDER"。下面给出了代码,每次它进入 catch 部分时,它都会启动 activity。如何打开它?
代码
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();
}
根据您的描述和错误信息,您需要使用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();
}
}
这是您可以查看的同一个主题:
问题已解决,
** 请按照本博客中所写的所有步骤进行操作,只需替换 try catch 块即可解决问题。
try
{
string extension = MimeTypeMap.GetFileExtensionFromUrl(Android.Net.Uri.FromFile(file).ToString());
string mimeType = MimeTypeMap.Singleton.GetMimeTypeFromExtension(extension);
Intent intent = new Intent(Intent.ActionView);
intent.SetFlags(ActivityFlags.ClearTop | ActivityFlags.NewTask);
Android.Net.Uri path = FileProvider.GetUriForFile(Application.Context, Android.App.Application.Context.PackageName + ".provider", file);
intent.SetDataAndType(path, mimeType);
intent.AddFlags(ActivityFlags.GrantReadUriPermission);
Application.Context.StartActivity(Intent.CreateChooser(intent, "Choose App"));
}
catch (Exception ex)
{
Toast.MakeText(Application.Context, "problem", ToastLength.Long).Show();
}