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

I created a .pdf file in the private local directory I got via (I try to work with the minimum of permissions):

string targetBaseDir = Environment.GetFolderPath(
    Environment.SpecialFolder.Personal,
    Environment.SpecialFolderOption.Create
    );

The official Android documentation suggests that file should be shared via a FileProvider.

I also tried to get some code to start an intent, and I'm currently at:

var fileUri = Android.Net.Uri.Parse(filePath);

var intent = new Intent();
intent.SetFlags(ActivityFlags.ClearTop);
intent.SetFlags(ActivityFlags.NewTask); 
intent.SetAction(Intent.ActionView);
intent.SetType("application/pdf");
intent.PutExtra(Intent.ExtraStream, fileUri);
intent.AddFlags(ActivityFlags.GrantReadUriPermission);
Android.App.Application.Context.StartActivity(intent);

This starts the share dialog for a pdf file but while the Adobe Pdf reader gets opened it shows an empty view and not my pdf file.

1 answer

  • answered 2018-11-07 11:10 Access Denied

    You need to wrap your URI with FileProvider. Since android uri will give you file:// while FileProvider will give you content://, which you actually need:

    public static Android.Net.Uri WrapFileWithUri(Context context,Java.IO.File file)
    {
        Android.Net.Uri result;
        if (Build.VERSION.SdkInt < (BuildVersionCodes)24)
        {
            result = Android.Net.Uri.FromFile(file);
        }
        else
        {
            result = FileProvider.GetUriForFile(context, context.ApplicationContext.PackageName + ".provider", file);
        }
        return result;
    }
    

    File can be createed this way:

    var file = new Java.IO.File(filePath); 
    

    Then you can open it:

        public static void View(Context context, string path, string mimeType) 
        {
            Intent viewIntent = new Intent(Intent.ActionView);
            Java.IO.File document = new Java.IO.File(path);            
            viewIntent.SetDataAndType(UriResolver.WrapFileWithUri(context,document),mimeType);            
            viewIntent.SetFlags(ActivityFlags.NewTask);
            viewIntent.AddFlags(ActivityFlags.GrantReadUriPermission);
            context.StartActivity(Intent.CreateChooser(viewIntent, "your text"));
        }
    

    Be aware, that this line

    viewIntent.SetDataAndType(UriResolver.WrapFileWithUri(context,document),mimeType);
    

    does not equal to SetData and SetType separate commands.

    And yes, you need to add FileProvider to your manifest:

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

    Also you need to create Resources\xml folder with provider_paths.xml file:

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