Xamarin Forms File Provider not set

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-30 18:09:23

The readme says:

If your application targets Android N (API 24) or newer, you must use version 2.6.0+.

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

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

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

YOUR_APP_PACKAGE_NAME must be set to your app package name!

2.) Add a new folder called xml into your Resources folder and add a new XML file called file_paths.xml

Add the following code:

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

YOUR_APP_PACKAGE_NAME must be set to your app package name!

You can read more at: https://developer.android.com/training/camera/photobasics.html

In my case it was because I had put the FileProvider in the wrong place in the AndroidManifest.xml file. It needed to be inside the <Application> tags.

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.myapp.app1" android:installLocation="auto">
        <uses-sdk android:minSdkVersion="22" android:targetSdkVersion="27" />
        <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
        <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
        <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
        <uses-permission android:name="android.permission.ACCESS_SURFACE_FLINGER" />
        <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
        <uses-permission android:name="android.permission.BROADCAST_WAP_PUSH" />
        <uses-permission android:name="android.permission.CAMERA" />
        <uses-permission android:name="android.permission.INTERNET" />
        <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
        <uses-permission android:name="android.permission.RECEIVE_WAP_PUSH" />
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
        <uses-permission android:name="android.permission.WRITE_SETTINGS" />
        <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
        <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
        <application android:label="app1.Android">
            <meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="AIzaSyCZF1O9_7-MXlxYfAEIxBQkr-dR8RiwiYE" />

            <!-- ADD FILE PROVIDER HERE -->
            <provider android:name="android.support.v4.content.FileProvider"
                  android:authorities="com.myapp.app1.fileprovider"
                  android:exported="false"
                  android:grantUriPermissions="true">
              <meta-data android:name="android.support.FILE_PROVIDER_PATHS"
              android:resource="@xml/file_paths"></meta-data>
            </provider>

        </application>
    </manifest>

The problem I had was related to case-sensitivity.

On the line below

<provider android:name="android.support.v4.content.FileProvider"
          android:authorities="com.myexample.myapp.fileProvider"
          android:exported="false"
          android:grantUriPermissions="true">

I erroneously had a single uppercase letter on the line android:authorities="com.myexample.myapp.fileProvider". It should be:

android:authorities="com.myexample.myapp.fileprovider"  <!-- all lowercase -->

Or

android:authorities="${applicationId}.fileprovider"    

The package name and value for android:authorities should be lowercase. Otherwise you will get the error:

System.ArgumentException: 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.

With the latest version, correct XML configuration is (without adding the package name to the path)

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

If you set path combining the package name like below

<external-path name="my_images" path="Android/data/YOUR_APP_PACKAGE_NAME/files/Pictures" />

It may give this type of error.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!