maui
maui copied to clipboard
.NET MAUI does not work with Android 13 Android.permission.write_external_storage
Description
My App was working and able to take pictures on Android 12 and .NET 6 but as soon as I upgraded to Android 13 and .NET 7, it stopped working. When I call this method:
FileResult photo = await MediaPicker.Default.CapturePhotoAsync();
I get this error: Microsoft.Maui.ApplicationModel.PermissionException: 'StorageWrite permission was not granted: Denied'.
I do have these declarations in the AndroidManifest.xml file:
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<queries>
<intent>
<action android:name="android.media.action.IMAGE_CAPTURE" />
</intent>
</queries>
Steps to Reproduce
- Create a new .NET MAUI App with the default template (not empty template).
- Replace the code in the OnCounterClicked method with the code below.
- Connect your device to your PC (my device is Google Pixel Pro 6).
- Run (Debug) the app on the local Android 13 device.
- Click the Counter button
- Notice the error
Link to public reproduction project repository
https://github.com/FatCodeMonkey/MAUIPhoto
Version with bug
6.0.486 (current)
Last version that worked well
6.0.424
Affected platforms
Android
Affected platform versions
Android 13
Did you find any workaround?
No
Relevant log output
0xFFFFFFFFFFFFFFFF in Android.Runtime.JNIEnv.monodroid_debugger_unhandled_exception C#
0x1A in Android.Runtime.JNINativeWrapper._unhandled_exception at /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:12,5 C#
0x1D in Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PP_V at /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:23,26 C#
0x17 in System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw C#
0x6 in System.Threading.Tasks.Task.<>c.<ThrowAsync>b__128_0 C#
0xC in Android.App.SyncContext. at /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.App/SyncContext.cs:36,19 C#
0xE in Java.Lang.Thread.RunnableImplementor.Run at /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Java.Lang/Thread.cs:36,6 C#
0x8 in Java.Lang.IRunnableInvoker.n_Run at /Users/runner/work/1/s/xamarin-android/src/Mono.Android/obj/Release/net6.0/android-33/mcw/Java.Lang.IRunnable.cs:84,4 C#
0x8 in Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PP_V at /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:22,5 C#
@FatCodeMonkey does it work if you add android:requestLegacyExternalStorage="true" to your manifest? <application android:allowBackup="true" android:icon="@mipmap/appicon" android:roundIcon="@mipmap/appicon_round" android:supportsRtl="true" android:requestLegacyExternalStorage="true">>
We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.
@mattleibow @rachelkang
What baffles me, is that .net MAUI was released with an initial target Android API of 33. But the underlying code does not support Android API 33!
Starting from Android 13, if your application target SDK is specified to 33 or above, the READ_EXTERNAL_STORAGE permission will be completely useless, and applying for it will have no effect.
Correspondingly, Google has added three runtime permissions: READ_MEDIA_IMAGES, READ_MEDIA_VIDEO, and READ_MEDIA_AUDIO, which are used to manage the photos, videos and audio files of the phone respectively. That is to say, in the past, you only needed to apply for a READ_EXTERNAL_STORAGE permission. This is no longer possible. You have to apply for it on demand, so that users can learn more precisely which media permissions your app has applied for.
<manifest>
<!-- Required only if your app targets Android 13. -->
<!-- Declare one or more the following permissions only if your app needs
to access data that's protected by them. -->
<uses-permission android:name="android.permission.READ_MEDIA_IMAGES" />
<uses-permission android:name="android.permission.READ_MEDIA_AUDIO" />
<uses-permission android:name="android.permission.READ_MEDIA_VIDEO" />
<!-- Required to maintain app compatibility. -->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"
android:maxSdkVersion="32" />
</manifest>
It is the same for notifications!
<manifest >
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
</manifest>
I pulled the Android permissions code, and updated it to support API 33. Hopefully they will realize that making the "Essentials" an actual part of the MAUI core, requires a higher level of attention to external API changes.
@PureWeen @UkeHa
@Stedy59 Could you share your modifications while we wait for the MAUI team to fix this ? (right now as a workaround I fixed the target android sdk to 30 in the android manifest).
I do not see how I can circumvent the issue either. Everyone around me has devices with the latest Android installed which I can't run my .net maui app on. The app can't work without media picker (MediaPicker control), but it seems to rely on WRITE_EXTERNAL_STORAGE only, which is not in the list of android permissions anymore. It does not care about new api 33 permissions like READ_MEDIA_IMAGES, it wants WRITE_EXTERNAL_STORAGE and does not run camera without it. Am I stuck till the issue is fixed in new ver of .net maui core, or there is some temp. workaround for that?
@YMichurin Unless you need what api 33 need, change the target SDK inside the android manifest to target api level 32. That did it for me
hi @sidewinder94, if I do change the manifest to api 32 and let's say I do not use api 33 features, would my app work on a phone with the newest api 33 installed?
Here is a snippet that I use to open the device gallery and/or select a ringtone...
[assembly: UsesPermission(Name = "android.permission.READ_EXTERNAL_STORAGE", MaxSdkVersion = 32)]
[assembly: UsesPermission(Name = "android.permission.READ_MEDIA_AUDIO")]
[assembly: UsesPermission(Name = "android.permission.READ_MEDIA_IMAGES")]
[assembly: UsesPermission(Name = "android.permission.READ_MEDIA_VIDEO")]
namespace StedySoft.Shared {
#region Class MediaServices
public static partial class MediaServices {
#region Declarations
internal const int OPENGALLERY = 100;
internal const int OPENRINGTONES = 101;
#endregion
#region Private Methods
private static bool PlatformGetExternalStoragePermissions(string requestMessage, int permissionType = OPENGALLERY) {
try {
Permission status = Permission.Denied;
if (Build.VERSION.SdkInt >= BuildVersionCodes.Tiramisu) {
switch (permissionType) {
case OPENGALLERY:
status = ContextCompat.CheckSelfPermission(Application.Context, Manifest.Permission.ReadMediaImages);
break;
case OPENRINGTONES:
status = ContextCompat.CheckSelfPermission(Application.Context, Manifest.Permission.ReadMediaAudio);
break;
}
}
else {
status = ContextCompat.CheckSelfPermission(Application.Context, Manifest.Permission.ReadExternalStorage);
}
if (status != Permission.Granted) {
if (Build.VERSION.SdkInt >= BuildVersionCodes.Tiramisu) {
switch (permissionType) {
case OPENGALLERY:
if (ActivityCompat.ShouldShowRequestPermissionRationale(Platform.Activity, Manifest.Permission.ReadMediaImages)) { Toast.Show(requestMessage, ToastDuration.Long); }
ActivityCompat.RequestPermissions(Platform.Activity, new[] { Manifest.Permission.ReadMediaImages }, 0);
break;
case OPENRINGTONES:
if (ActivityCompat.ShouldShowRequestPermissionRationale(Platform.Activity, Manifest.Permission.ReadMediaAudio)) { Toast.Show(requestMessage, ToastDuration.Long); }
ActivityCompat.RequestPermissions(Platform.Activity, new[] { Manifest.Permission.ReadMediaAudio }, 0);
break;
}
}
else {
if (ActivityCompat.ShouldShowRequestPermissionRationale(Platform.Activity, Manifest.Permission.ReadExternalStorage)) { Toast.Show(requestMessage, ToastDuration.Long); }
ActivityCompat.RequestPermissions(Platform.Activity, new[] { Manifest.Permission.ReadExternalStorage }, 0);
}
Stopwatch stopwatch = new();
stopwatch.Start();
do {
Thread.Sleep(10);
if (Build.VERSION.SdkInt >= BuildVersionCodes.Tiramisu) {
switch (permissionType) {
case OPENGALLERY:
status = ContextCompat.CheckSelfPermission(Application.Context, Manifest.Permission.ReadMediaImages);
break;
case OPENRINGTONES:
status = ContextCompat.CheckSelfPermission(Application.Context, Manifest.Permission.ReadMediaAudio);
break;
}
}
else {
status = ContextCompat.CheckSelfPermission(Application.Context, Manifest.Permission.ReadExternalStorage);
}
if (stopwatch.ElapsedMilliseconds > 10000) {
throw new Exception("Timed out waiting for permission.");
}
} while (status != Permission.Granted);
}
return true;
}
catch (Exception ex) {
Logger.LogException("MediaServices", "PlatformGetExternalStoragePermissions", ex);
}
return false;
}
@YMichurin Yes, no issues there, since devices running higher versions know how to deal with older ones.
And here is the code
<uses-sdk android:minSdkVersion="21" android:targetSdkVersion="32" />
Same here. I had to set the targetSdkVersion to 32 to use Notifications. A lot of people already user Android 13. It would be nice to have the new permissions implemented in Maui.
I have the same issue. The question now is, how long will it take for them to release an update to fix this. Releasing updates seems to be taking a really long time.
I avoided this issue by using Xamarin.MediaGallery instead.
https://github.com/dimonovdd/Xamarin.MediaGallery
I avoided this issue by using Xamarin.MediaGallery instead.
https://github.com/dimonovdd/Xamarin.MediaGallery
I tried this, and followed the readme file, with both the stable and preview release, but it just pops up a dialog saying "Can't connect to the camera" in the Android 13 Emulator with SDK level 33.
Are you on .NET6 like the sample? Is there some other trick to it?
@symbiogenesis, again, if you don't need to target sdk33 just target 32 and it'll work just fine. Still a bug that needs to be fixed.
@symbiogenesis, again, if you don't need to target sdk33 just target 32 and it'll work just fine. Still a bug that needs to be fixed.
Thank you. I had tried this before and it didn't work for some reason, but I tried it now and it works. This is the best solution.
I'm having the same problem. I'm following the colleague's last suggestion, that is, changing the destination to sdk 32. Previously, I had used Xam.Media.Plugin, however, when I open the camera, vs closes and the app in debug mode also closes without throwing the error message.
@FatCodeMonkey does it work if you add android:requestLegacyExternalStorage="true" to your manifest? <application android:allowBackup="true" android:icon="@mipmap/appicon" android:roundIcon="@mipmap/appicon_round" android:supportsRtl="true" android:requestLegacyExternalStorage="true">>
After you update your app to target Android 11, the system ignores the requestLegacyExternalStorage flag. [source]
So no, that's a completely different option that only worked before Android 11.
And here is the code
<uses-sdk android:minSdkVersion="21" android:targetSdkVersion="32" />
How do you get that to work? If I do that I get:
warning XA4211: AndroidManifest.xml //uses-sdk/@android:targetSdkVersion '32' is less than $(TargetFrameworkVersion) ''. Using API-33 for ACW compilation.
@espenrl The compilation still happens with API 33 and the MediaPicker.Capture methods won't work.
Because the warning mentions $(TargetFrameworkVersion), I tried adding a TargetFrameworkVersion option with value 32 to the build, but that didn't change a thing.
And here is the code
<uses-sdk android:minSdkVersion="21" android:targetSdkVersion="32" />How do you get that to work? If I do that I get:
Double check that Targeting Android option is on in the project settings and it targets lower version. This is what I have in the project file and it works. I do not think you need anything else behind that to avoid the error
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'android'">21.0</SupportedOSPlatformVersion>
And here is the code
<uses-sdk android:minSdkVersion="21" android:targetSdkVersion="32" />How do you get that to work? If I do that I get:
Double check that Targeting Android option is on in the project settings and it targets lower version. This is what I have in the project file and it works. I do not think you need anything else behind that to avoid the error
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'android'">21.0</SupportedOSPlatformVersion>
Hmm… yes, that option is already in our csproj file, it's needed to build Android. This sets the minSdkVersion.
I just ignored the XA4211 warning. It worked. I took photos successfully on Android 13, both with the real device and the emulator.
I think your PR is still very helpful, and I hope you continue to clean up that horrible code.
The iOS code is possibly even worse. The photos don't respect the device orientation, and it is using obsolete APIs with lots of TODOs and pragmas in the code.
I just ignored the XA4211 warning. It worked. I took photos successfully on Android 13, both with the real device and the emulator.
I tried, but the message is on point. It's still built using API 33 and therefore the error still occurs. Is it possible you ignored a similar, but different, warning?
I still have that warning in my code right now, and it is the only one. Thinking again, I think it is possible that I only verified it using the emulator, and took the device photos with an ipad.
I recently had tried switching to the Xamarin.MediaGallery nuget package (with MAUI support) because of a different ios camera bug regarding device orientation data not getting saved in the EXIF metadata. It seemed to have some recent work done for Android 13 compatibility as well, so it seemed promising.
That completely didn't work for me at all, so I tried switching back to MAUI essentials plus a workaround, and that seems to have fixed ios. But the Android version isn't even launching now on a real device with a published app in Release mode. So I cannot really verify anything about this right now, except to say that it is working perfect in the emulator for Android SDK 33
.
Actually, I'm now seeing 3 warnings in the error list

I do see those too, note that the third error seems to be a re-parse of the second one, where the Code is parsed separately from the Description by Visual Studio. I'm just seeing the two unique warnings.
Ok, I managed to find it, and it has to do with a really unexpected twist in how certain values in the android manifest are applied.
I followed this thread, and applied appropriate changes to the manifest, such as the relevant parts of this comment and this comment.
Therefore my android manifest ended up with these two lines (other lines omitted):
<uses-sdk android:minSdkVersion="21"
android:targetSdkVersion="32" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="32" />
This seems correct. I want to build with support for API 21 - 32, and on all those API versions WRITE_EXTERNAL_STORAGE exists, is required, and is requested. If I ever upgrade to building for API 33, WRITE_EXTERNAL_STORAGE should not be requested for that API 33 version, because it doesn't exist in API 33.
In this case I set android:targetSdkVersion="32", and I get the warning that it compiles with API 33 after all. Apparently this means that the uses-permission is omitted, because we're at API 33. If I remove the android:maxSdkVersion attribute from the uses-permission line, I still build API 33 according to the warning, but now suddenly uses-permission does work correctly, even though that permission doesn't even exist in API 33.
I'm baffled, yet happy that our release-blocker can be bypassed this way.
<uses-permission android:name="android.permission.READ_MEDIA_IMAGES" />
<uses-permission android:name="android.permission.READ_MEDIA_AUDIO" />
<uses-permission android:name="android.permission.READ_MEDIA_VIDEO" />
<uses-permission android:name="android.permission.CAMERA" />
<queries>
<intent>
<action android:name="android.media.action.IMAGE_CAPTURE" />
</intent>
</queries>
This is how my AndroidManifest.xml looks like and its still not working any ideas?
<uses-permission android:name="android.permission.READ_MEDIA_IMAGES" /> <uses-permission android:name="android.permission.READ_MEDIA_AUDIO" /> <uses-permission android:name="android.permission.READ_MEDIA_VIDEO" /> <uses-permission android:name="android.permission.CAMERA" /> <queries> <intent> <action android:name="android.media.action.IMAGE_CAPTURE" /> </intent> </queries>This is how my AndroidManifest.xml looks like and its still not working any ideas?
You haven't yet told us what you're trying to do. What exactly is not working? Are you trying to take a picture on Android 13? Then reread this thread, because that's discussed quite extensively.
You can remove the READ_MEDIA permissions, since taking pictures is not about reading, but writing. Then you'll need:
<uses-sdk android:minSdkVersion="21"
android:targetSdkVersion="32" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Although you may use a different minSdkVersion.
Keep in mind that this is not necessary because of Android, but because of a bug in MAUI. Once #12766 is merged and a new MAUI version is released, it's no longer necessary.