Search code examples
xamarinxamarin.formsxamarin.essentialsplugin.media.crossmedia

Xamarin Essentials Permissions failing to grant permission


I'm currently writing a Xamarin Forms app which requires use of the camera, in the code below I am requesting the permission using the Xamarin Essentials Permissions which comes back as "Granted"; immediately following that I am requesting use of the camera to take a photo, which throws the following error.

ex = {Plugin.Media.Abstractions.MediaPermissionException: Camera permission(s) are required.

The permission code

public static async Task<bool> GetPermission<TPermission>() where TPermission : BasePermission, new()
    {
        var hasPermission = await Permissions.CheckStatusAsync<TPermission>();

        if (hasPermission == PermissionStatus.Granted)
            return true;
        else if (hasPermission == PermissionStatus.Disabled)
            return false;

        var result = await Permissions.RequestAsync<TPermission>();
        if (result != PermissionStatus.Granted)
            return false;

        return true;
    }

The photo manager code

if(!await PermissionHelpers.GetPermission<Permissions.Camera>())
        {
            await new ErrorAlert().Show("App can't take a picture without permission to use the camera");
            return string.Empty;
        }

        var photo = await CrossMedia.Current.TakePhotoAsync(new StoreCameraMediaOptions
        {
            PhotoSize = PhotoSize.Small,
            SaveToAlbum = false
        });

As previously said, the GetPermission method returns true, but still the error is thrown.

I'm currently running this on Android. My AndroidManifest.xml has these permission in it.

<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" />

I have now made a sample project to showcase my issue GitHub Repo for the issue


Solution

  • thank you for all of your time gone into helping me to resolve this issue. It turned out that if you are using Xamarin essentials version 1.5.0 you need to install the CurrentActivity NuGet plugin to your android project.

    Or, a better solution is update to 1.5.1 which resolves the issue entirely.