在Xamarin.Android中获取文件的content-uri的最佳实践是什么?

时间:2019-02-12 12:20:14

标签: xamarin xamarin.android android-fileprovider

我已经通过以下方式在清单中注册了FileProvider:

<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>

现在,我想获取该文件的URI,以传递给另一个应用程序以打开该文件。我尝试过:

Intent viewIntent = new Intent(Intent.ActionView);
Java.IO.File document = new Java.IO.File(filePath);
Android.Net.Uri contentUri = FileProvider.GetUriForFile(
    _context, Android.Support.FILE_PROVIDER_PATHS, document);

viewIntent.SetDataAndType(contentUri, GetMimeType(document));
viewIntent.SetFlags(ActivityFlags.NewTask);
viewIntent.AddFlags(ActivityFlags.GrantReadUriPermission);
Intent chooser = Intent.CreateChooser(viewIntent,
    "Which program shall open the file?");
chooser.SetFlags(ActivityFlags.NewTask);
chooser.AddFlags(ActivityFlags.GrantReadUriPermission);

_context.StartActivity(chooser);

不幸的是,我似乎无法以此方式解决Android.Support.FILE_PROVIDER_PATHS。如何获得与possibvle一样干净的URI?

1 个答案:

答案 0 :(得分:0)

_context.PackageName + ".provider"完成任务(_contextAndroid.Content.Context):

Intent viewIntent = new Intent(Intent.ActionView);
Java.IO.File document = new Java.IO.File(filePath);
Android.Net.Uri contentUri = FileProvider.GetUriForFile(
    _context, Android.Support.FILE_PROVIDER_PATHS, document);

viewIntent.SetDataAndType(contentUri, GetMimeType(document));
viewIntent.SetFlags(ActivityFlags.NewTask);
viewIntent.AddFlags(ActivityFlags.GrantReadUriPermission);
Intent chooser = Intent.CreateChooser(viewIntent,
    "Which program shall open the file?");
chooser.SetFlags(ActivityFlags.NewTask);
chooser.AddFlags(ActivityFlags.GrantReadUriPermission);

_context.StartActivity(chooser);