Photo select from storage does not work on Android 13 from Xamar

ghz 7months ago ⋅ 58 views

Photo select from storage does not work on Android 13 from Xamarin Forms application

We have an requirement where we need to select images from android storage, so in Xamarin Forms application we have added following code,

public async Task<bool> CheckAndRequestStoragePermission()
    {
        PermissionStatus status = PermissionStatus.Unknown;

        bool isPermissionChecked = false;

        Version version = DeviceInfo.Version;

        if (DeviceInfo.Platform == DevicePlatform.Android)
        {
            if (version.Major >= 12)
            {
                isPermissionChecked = true;

                status = await Permissions.CheckStatusAsync<Permissions.Photos>();

                if (status != PermissionStatus.Granted)
                {
                    status = await Permissions.RequestAsync<Permissions.Photos>();
                }
            }
        }

        if (isPermissionChecked == false)
        {
            status = await Permissions.CheckStatusAsync<Permissions.StorageRead>();

            if (status != PermissionStatus.Granted)
            {
                status = await Permissions.RequestAsync<Permissions.StorageRead>();
            }
        }

        return status == PermissionStatus.Granted;
    }

    private async void OnChooseImageButtonClicked(object sender, EventArgs e)
    {
        try
        {
            await CheckAndRequestStoragePermission();

            var photo = await MediaPicker.PickPhotoAsync();
            if (photo != null)
            {
                imagePathList.Add(photo.FullPath);
                selectedImages.Add(new SelectedImage { ImagePath = photo.FullPath });
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine($"Error picking image: {ex.Message}");
        }
    }

Also added required permissions in Android manifest file as,

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

But on Android device, application does not ask for the permissions and "status = await Permissions.RequestAsync<Permissions.StorageRead>();" auto returns with Denied permission. Also it throws an exception as following line,

var photo = await MediaPicker.PickPhotoAsync();

and exception is about StorageRead permission.

Please guide us how to make it work on Android version 13 devices.

Answers

It seems like you're encountering issues with requesting permissions and picking photos on Android 13 devices in your Xamarin Forms application. Here are some steps you can take to troubleshoot and resolve the issue:

  1. Check Android 13 Changes: Android 13 may have introduced changes to permissions or storage access. Make sure to review the Android 13 documentation and release notes to understand any changes that may affect your app's functionality related to permissions and storage access.

  2. Update Xamarin.Essentials: Ensure that you are using the latest version of Xamarin.Essentials library, which provides a simplified cross-platform API for accessing device features, including permissions. Newer versions may have fixes or updates related to Android 13 compatibility.

  3. Verify Manifest Permissions: Double-check your Android manifest file to ensure that the required permissions are correctly declared. You've mentioned adding permissions for READ_EXTERNAL_STORAGE and READ_MEDIA_IMAGES, but it's also important to ensure that they are declared correctly.

  4. Handle Permission Results: After requesting permissions, make sure to handle the permission status properly. If the permission request is denied, you may need to display a message to the user explaining why the permission is required and prompt them to grant the permission manually through the device settings.

  5. Check Permission Group: Verify that the Permissions.StorageRead permission group is correct for accessing storage. It's possible that the permission group name may have changed in newer versions of Xamarin.Essentials or Android, so make sure to use the correct permission group.

  6. Test on Emulator and Real Devices: Test your app on both emulators and real devices running Android 13 to ensure that the issue is not specific to a particular device or configuration. This can help identify whether the problem is related to the device setup or the application code.

  7. Debugging: Use debugging tools provided by Xamarin and Android Studio to debug the application and identify any errors or exceptions that occur during permission requests or photo picking. This can provide valuable insights into what might be causing the issue.

  8. Community and Forums: Seek help from the Xamarin community forums or other developer communities where you can discuss the issue with other developers who may have encountered similar problems or have insights into potential solutions.

By following these steps and troubleshooting the issue systematically, you should be able to identify the root cause of the problem and implement the necessary fixes to ensure that your Xamarin Forms application works correctly on Android 13 devices.