2

I am using the Xamarin.Essentials MediaPicker for capturing images in my app. https://learn.microsoft.com/de-de/xamarin/essentials/media-picker?tabs=android

just like var photo = await MediaPicker.CapturePhotoAsync();

For Android < 13 I am asking for permissions:

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

and everything works fine.

Since those permission do not work for Android 13, I am asking for

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

(see Android 13 - How to request WRITE_EXTERNAL_STORAGE)

the problem is, that the Xamarin.Essentials.MediaPicker still misses the StorageWritePermission when trying to take a photo. But I can't ask for those in Android 13.

enter image description here

Any ideas how to make it work for Android 13?

Info:

Update:

Aiko West
  • 791
  • 1
  • 10
  • 30

2 Answers2

0

Update

In Android 13, the method MediaPicker.CapturePhotoAsync() can not be used due to the permission. You can check the issue thread Storage permissions Android 13 api 33 for more information.

The code below works for versions prior to Android 13

I created a demo on my side and it worked well, you can refer to this. I am using the permissions below.

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

 <queries>
    <intent>
        <action android:name="android.media.action.IMAGE_CAPTURE" />
    </intent>
</queries>

Here is the code in .cs file.

 public partial class MainPage : ContentPage
        {
            public string PhotoPath { get; set; } = null;
            public MainPage()
            {
                InitializeComponent();
            }
    
            private async void Button_Clicked(object sender, EventArgs e)
            {
                var file = await MediaPicker.CapturePhotoAsync();
                await LoadPhotoAsync(file);
                if (file == null)
                    return;
                async Task LoadPhotoAsync(FileResult photo)
                {
                    // canceled
                    if (photo == null)
                    {
                        PhotoPath = null;
                        return;
                    }
                    // save the file into local storage
                    var newFile = Path.Combine(FileSystem.CacheDirectory, photo.FileName);
                    using (var stream = await photo.OpenReadAsync())
                    using (var newStream = File.OpenWrite(newFile))
                        await stream.CopyToAsync(newStream);
                    PhotoPath = newFile;
                    img.Source = PhotoPath;//Show on the front page
                }
            }
        }

Here is the code in xaml

<StackLayout>
    <Button Text="button" Clicked="Button_Clicked"></Button>
    <Image x:Name="img" ></Image>
</StackLayout>
Guangyu Bai - MSFT
  • 2,555
  • 1
  • 2
  • 8
  • I tested the `MediaPicker.CapturePhotoAsync()` , you need to add the ` ` in the AndroidManifest.xml file. – Guangyu Bai - MSFT Jan 04 '23 at 08:17
  • this does not work on my Samsung Tab 7 FE Android 13. I even cannot ask for the WRITE_EXTERNAL_STORAGE permission. There won't come the popup to grant the permission – Aiko West Jan 04 '23 at 08:32
  • ..... but those permission should not be needed in Android 13, but the CapterPhotoAsync still asks for them – Aiko West Jan 04 '23 at 08:38
  • 1
    This seems an issue on the android 13 you can check the issue thread [Storage permissions Android 13 api 33](https://github.com/xamarin/Essentials/issues/2041). – Guangyu Bai - MSFT Jan 04 '23 at 08:57
0

Fixed in Xamarin.Essentials Nuget Update 1.7.5

No problem on my Android 13 devices after updating the NugetPackage

enter image description here

Aiko West
  • 791
  • 1
  • 10
  • 30