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" />
Ok I Fixed it by changing the saving location to System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal)
Don't ask me why that worked when they need the same permissions but it did.