FileProvider.GetUrlForFile()不适用于Android版本8

时间:2018-12-24 13:24:57

标签: android xamarin.android android-fileprovider

我正在Xamarin.Android中工作。有一个模块,我需要将PDF或DOC文件下载到移动下载文件夹中,然后立即需要打开文件。下载功能正常。

我正在尝试使用第三方应用程序打开下载的文件。我已经实现了以下代码。

包括/FragmentOfferDetails.cs的第300-310行

Intent intent = new Intent (Intent.ActionView);
var documentsPath =     System.Environment.GetFolderPath     (System.Environment.SpecialFolder.Personal);
string filePath = Path.Combine (documentsPath, file);
Android.Net.Uri uri = FileProvider.GetUriForFile (this.Context, Application.Context.PackageName + ".fileprovider", new Java.IO.File (filePath));

if (uri != null) {
    intent.SetFlags (Android.Content.ActivityFlags.GrantReadUriPermission);
} else {
    uri = Android.Net.Uri.FromFile (new Java.IO.File (filePath));
}
intent.SetDataAndType (uri, attachment.mimeType);
intent.SetFlags (ActivityFlags.ClearWhenTaskReset || ActivityFlags.NewTask);
Intent intentChooser = Intent.CreateChooser (intent, "Open File");
this.StartActivity (intentChooser);

provider_paths.xml

<?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="files" path="."/>
       <internal-path name="internal_files" path="."/>
  </paths>

AndroidManifest.xml

内部应用程序标记

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

我希望pdf查看器将打开特定的pdf文件。

但是,我在此行遇到异常

Android.Net.Uri uri = FileProvider.GetUriForFile (this.Context, Application.Context.PackageName + ".fileprovider", new Java.IO.File (filePath));

,唯一的例外是:

  

Java.Lang.RuntimeException:尝试在null上调用虚拟方法'android.content.res.XmlResourceParser android.content.pm.PackageItemInfo.loadXmlMetaData(android.content.pm.PackageManager,java.lang.String)'对象引用

我尝试了许多小的更改后的代码。但是我无法解决。

0 个答案:

没有答案