Opening a PDF in Default Application

I have followed all the directions for opening a pdf from downloads but I always get an error that it cannot access the file. I have read storage on, how do I start an intent to open the default file handler for a variety of files without some security issue?

Answers

  • YorkGoYorkGo CNMember, Xamarin Team Xamurai

    I always get an error that it cannot access the file

    Please post the entire error trace.

    how do I start an intent to open the default file handler for a variety of files without some security issue?

    Requesting Runtime Permissions in Android Marshmallow+.

  • YorkGoYorkGo CNMember, Xamarin Team Xamurai

    Did this issue only happened on Android 7.0+ device? If so, maybe this issue is related to FileProvider. The file provider issue should be something like the following:

    Unable to get file location. This most likely means that the file provider information is not set in your Android Manifest file. Please check documentation on how to set this up in your project.01-18 16:35:19.171 I/mono-stdout(22873): Unable to get file location. This most likely means that the file provider information is not set in your Android Manifest file. Please check documentation on how to set this up in your project.

    Please refer to the Android document:

    We are using getUriForFile(Context, String, File) which returns a content:// URI. For more recent apps targeting Android 7.0 (API level 24) and higher, passing a file:// URI across a package boundary causes a FileUriExposedException. Therefore, we now present a more generic way of storing images using a FileProvider.

    Now, you need to configure the FileProvider...

    You must add a few additional configuration files to adhere to the new strict mode:

    1.) Add the following to your AndroidManifest.xml inside the <application> tags:

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

    2.) Add a new folder called xml into your Resources folder and add a new XML file called file_paths.xml. Make sure that this XML file has a Build Action of: AndroidResource.

    Add the following code:

    <?xml version="1.0" encoding="utf-8"?>
    <paths xmlns:android="http://schemas.android.com/apk/res/android">
        <external-path name="Download" path="Download/"/>
    </paths>
    

    For more detailed information about FileProvider, you could refer to: Setting up file sharing

Sign In or Register to comment.