Forum Xamarin.Forms

Xamarin Essentials Permissions failing to grant permission

benpricebenprice Member ✭✭
edited March 10 in Xamarin.Forms

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.

Best Answer

Answers

  • bmgsmithbmgsmith Member ✭✭

    Just checking, since you did not mention this in your post. Did you enable the camera permissions in your Android Manifest?

  • benpricebenprice Member ✭✭

    Hi bmgsmith,

    • I have set up my Android Manifest with permissions for READ, WRITE, CAMERA and even tried adding a uses-feature for the camera too.
    • I have Xamarin essentials initialised with the on request permission result method set up in my Main Activity file.
    • I have my external paths set up in an xml folder.
    • And i'm running on an Android 7.0 device as opposed to a simulator.
    • I have also cleaned and reloaded visual studio 2019 far too many times at this point!
  • benpricebenprice Member ✭✭

    A new development with this issue, adding the code

    if (ContextCompat.CheckSelfPermission(this, Manifest.Permission.Camera) != (int)Permission.Granted) { RequestPermissions(new string[] { Manifest.Permission.Camera, Manifest.Permission.WriteExternalStorage, Manifest.Permission.ReadExternalStorage }, 0); }

    to the Main Activity on Android fixes the issue ONLY if the user accepts permissions on startup. If they deny permissions here and then try to use the camera, ideally we want to ask again but cannot as even if they say yes, TakePhotoAsync throws a permission error.

  • NicoleLuNicoleLu Member, Xamarin Team Xamurai

    According to your exception, you're using james MediaPlugin right?

    I went through a complete process to use the plugin, it works good, and actually you don't need to add/request the camera permission for the plugin to work as illustrated here in the tutorial.

    But you do need to follow the guideand make everything configured right, including a provider and an xml.

  • benpricebenprice Member ✭✭
    edited March 12

    I have been through the tutorials for it many times over, I also have a stack overflow post where people suggested individually each system from said tutorials, my reproduction steps are as follows:

    • Create your class with your permission and take photo methods inside of it, as well as the command to do so.
    • Add your class as a property to a view model bound to your main page (or other xaml.cs)
    • Create a custom view with a button that triggers the take photo command
    • Add the custom control to your main page (or other xaml) and bind it's binding context to your class property.

    I have also created a sample project displaying the issue here
    https://github.com/BenPrice851/IssueProjects

  • benpricebenprice Member ✭✭

    You, NicoleLu are an absolute lifesaver!
    The part I was missing was the CurrentActivity NuGet plugin, installing just that to my project made the entire system work as intended.
    Thank you very much for all of your help!

Sign In or Register to comment.