Xamarin : Android : System.UnauthorizedAccessException: Access to the path is denied

First of all add this permissions to you Manifest:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Since Android 6.0 (API 23) you need also to request the permissions manually, add this code on your MainActivity.cs on your Xamarin.Android project:

if ((ContextCompat.CheckSelfPermission(this, Manifest.Permission.WriteExternalStorage) != (int)Permission.Granted)
            || (ContextCompat.CheckSelfPermission(this, Manifest.Permission.ReadExternalStorage) != (int)Permission.Granted))
        {
            ActivityCompat.RequestPermissions(this, new string[] { Manifest.Permission.ReadExternalStorage, Manifest.Permission.WriteExternalStorage }, REQUEST);
        }

Since Android 10 you may also need to add android:requestLegacyExternalStorage attribute to your Manifest like this:

<application android:requestLegacyExternalStorage="true" />

UPDATE

After doing all this maybe you still get the exception on Android 11 or greather If you try save a file on any path of the SDCARD, you can only use the EXTERNAL STORAGE and INTERNAL STORAGE paths of your App by default.

PRIVATE EXTERNAL STORAGE PATH OF YOUR APP:

Android.App.Application.Context.GetExternalFilesDir(null).AbsolutePath

PRIVATE INTERNAL STORAGE PATH OF YOUR APP:

System.Environment.GetFolderPath(System.Environment.SpecialFolder.LocalApplicationData)

If you need to access to any path of the SDCARD on Android 11 or greather, you should ask for manage all files access permission.

private void RequestStorageAccess()
{
    if (!Environment.IsExternalStorageManager) 
    {
        StartActivityForResult(new Intent(Android.Provider.Settings.ActionManageAllFilesAccessPermission), 3);
    }
}

This will pop up an Activity listing all the Apps that have access to all file access permission, the user should tap your App and enable the permission.

But in order to make your App appear there, you should add this permission to your manifest:

<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"/>

And you also need to make sure that you are compiling your App with Android 11 or greather.

enter image description here

Here you can see the permission Activity:

enter image description here

enter image description here

For some reason I was able to save files to documents folder without this permission, but after deleting the files I was not able to create again the same files with the same name, also I was not able to list all the files, this is why this pop-up is needed even to use the documents directory.

Use manage all files permission only in case that you really need

If you want to publish your App in Google Play with the “Manage All Files Permission”, you will have to Fill a “Permissions Declaration Form” on Google Play, because it is considered a
“high-risk or sensitive permissions” here is more information, and Google should approve it:

https://support.google.com/googleplay/android-developer/answer/9214102?hl=en#zippy=

When to ask for this permission:

https://support.google.com/googleplay/android-developer/answer/10467955?hl=en

Leave a Comment