Forum Xamarin.Android
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

open pdf is not working in xmarine android

soumenhaldersoumenhalder Member ✭✭✭
edited February 17 in Xamarin.Android

Hi am new in xmarine android,I am working in a xmarin android c# project to open a pdf file which is in the mobile internal storage "testpdf" folder. a dummy pdf file is in the "testpdf" folder. The app is asking user to choose preferred pdf viewer apps to open that pdf. but after selecting the preferred apps pdf is not showing. I have followed the below steps:

add FileProvider to the app manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="" package="RunTimePerm.RunTimePerm" android:versionCode="1" android:versionName="1.0" android:installLocation="auto">
    <uses-sdk android:minSdkVersion="19" android:targetSdkVersion="27" />   
    <application android:allowBackup="true" android:label="@string/app_name" android:supportsRtl="true">
        <provider android:name="" android:authorities="${applicationId}.provider" android:exported="false" android:grantUriPermissions="true">
            <meta-data android:name="" android:resource="@xml/file_paths" />
  <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" />

also created Resources\xml folder with file_paths.xml

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="">
  <external-files-path name="root" path="."/>
  <external-files-path name="files" path="files" />
  <external-path name="external_files" path="."/>

then added the reference Xmarin.Android.Support.V4 from NuGet.

then the following code:

var externalPath = global::Android.OS.Environment.ExternalStorageDirectory.Path + "/testpdf";
                Java.IO.File file = new Java.IO.File(externalPath, "test.pdf");
                if (file.Exists())
                    var context = Android.App.Application.Context;
                        Android.Net.Uri pdfPath = Android.Support.V4.Content.FileProvider.GetUriForFile(context, context.ApplicationContext.PackageName + ".provider", file);
                        context.GrantUriPermission(context.PackageName, pdfPath, ActivityFlags.GrantReadUriPermission);
                        Intent intent = new Intent();
                        intent.SetFlags(ActivityFlags.ClearWhenTaskReset | ActivityFlags.NewTask);
                        intent.SetDataAndType(pdfPath, "application/pdf");
                    catch (Exception ex)
                        Toast.MakeText(Application.Context, "problem", ToastLength.Long).Show();

I have 3 options present in my mobile to open a pdf to test the scenario, i) wps office ii) Drive PDF viewer iii)Adobe Acrobat Reader.After opening wps office -- it is saying "wps office is determining the type of document" and PDF is not showing. In Drive PDF viewer-- after opening it is showing nothing. and in acrobat reader-- it is saying "file colud not be accessed. check your location" but the pdf is present in the correct to resolve?


  • soumenhaldersoumenhalder Member ✭✭✭

    got the solution.

                            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);
                            Application.Context.StartActivity(Intent.CreateChooser(intent, "Choose App"));
                        catch (Exception ex)
                            Toast.MakeText(Application.Context, "problem", ToastLength.Long).Show();
Sign In or Register to comment.