我正在尝试使用文件提供程序显示 PDF 文件。但是,当 PDF 阅读器打开时,它不包含文件的内容。该文件存储在应用程序的内部存储文件夹中。
list :
<provider android:name="android.support.v4.content.FileProvider" android:authorities="com.mycom.myapp.fileprovider" android:exported="false" android:grantUriPermissions="true">
<meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths" />
</provider>
文件路径:
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<files-path name="export" />
</paths>
pdfViewRenderer.cs:
string libraryPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
var path = Path.Combine(libraryPath, fileName);
string application = "application/pdf";
Java.IO.File javaFile = new Java.IO.File(fileName);//externalPath
Android.Net.Uri androidURI = FileProvider.GetUriForFile(Forms.Context.ApplicationContext, "com.mycom.myapp.fileprovider", javaFile);
Intent intent = new Intent(Intent.ActionView);
intent.SetDataAndType(androidURI, Filetype);
intent.AddFlags(ActivityFlags.GrantReadUriPermission);
intent.SetFlags(ActivityFlags.NoHistory);
intent.SetFlags(ActivityFlags.ClearWhenTaskReset);
Forms.Context.StartActivity(Intent.CreateChooser(intent, "Open PDF"));
最佳答案
您不能直接访问应用程序的 .apk
中的文件,“.apk”不会将自身呈现为文件系统。
您可以将 AndroidAsset
复制到您的应用程序缓存目录,然后将 FileProvider
权限授予基于文件的 Uri
以允许外部应用程序在您应用的沙箱中访问该文件。
在您的 AndroidManifest
的 application
元素中添加一个 provider
:
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.sushihangover.artificialgravitycalc"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/cache_path" />
然后将文件名与提供者的 android:resource:
匹配的 xml 文件添加到您的 Resources/xml
中,其中包含您授予访问权限的路径:
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<cache-path name="appcache" path="." />
</paths>
注意:您可能需要将 xml
目录添加到您的 Resources
文件夹,因为默认情况下它不会通过基本 Xamarin.Android
添加> 项目模板。 (而且必须命名为xml
,小写)
将您的 .pdf
添加到 Assets
目录并确保它被标记为 AndroidAsset
构建类型:
现在您可以将该 Asset
复制到应用的缓存目录,创建一个基于 FileProvider
的 Uri
,授予读取权限,然后尝试打开它。在这种情况下,您至少需要一个已安装的应用程序来发布它处理 application/pdf
mime 类型:
var pdfAssetName = "PlayScriptLanguageSpecification.pdf";
var savePath = Path.GetTempFileName();
using (var assetStream = Application.Context.Assets.Open(pdfAssetName))
using (var fileStream = new FileStream(savePath, FileMode.Create, FileAccess.Write))
{
byte[] buffer = new byte[1024];
int readCount = 0;
do
{
readCount = assetStream.Read(buffer, 0, buffer.Length);
fileStream.Write(buffer, 0, readCount);
} while (readCount > 0);
}
var targetIntent = new Intent(Intent.ActionView);
var file = new Java.IO.File(savePath);
var uri = FileProvider.GetUriForFile(this, PackageName, file);
targetIntent.SetDataAndType(uri, "application/pdf");
targetIntent.SetFlags(ActivityFlags.NoHistory);
targetIntent.SetFlags(ActivityFlags.GrantReadUriPermission);
Intent intent = Intent.CreateChooser(targetIntent, "Open File");
StartActivity(intent);
关于c# - FileProvider Xamarin 不显示文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40462245/