How do I get Camera permission using Media Plugin?

hkiddhkidd Member ✭✭

After following all the set-up directions and implementing the following code block to check camera permissions, I am still getting the DisplayAlert that permissions are denied for the camera.
// verifies that the user has permission to access the camera var cameraStatus = await CrossPermissions.Current.CheckPermissionStatusAsync(Permission.Camera); if (cameraStatus != PermissionStatus.Granted) { var results = await CrossPermissions.Current.RequestPermissionsAsync(new[] { Permission.Camera }); cameraStatus = results[Permission.Camera]; if (cameraStatus != PermissionStatus.Granted) { await DisplayAlert("Permissions Denied", "Unable to take photos.", "OK"); CameraButton.IsEnabled = false; return; } }
I am developing a cross-platform application in Xamarin, using .NET 2.0 Standard. I have the permissions and media plugins installed, but cannot figure out where I am going wrong. I am testing on an "Android_Accelerated_x86_Oreo (Android 8.1 - API 27)" with the hardware for camera set to true in the ADM.
The repository I am working in is a git enterprise repo called "Camera POC" and is a simple application that only needs to open the camera using the button created, take a picture, and display it (photo storage is out of scope).
Do you have any ideas of why I am unable to gain permission to the camera and how I could overwrite that?

Best Answer

  • hkiddhkidd ✭✭
    Accepted Answer

    Update: I just had to add a block of code that redirected the user to the OpenAppSettings() and then navigate back to the app and it gave me permission

Answers

  • Saraswati_ChandraSaraswati_Chandra USMember ✭✭✭

    @hkidd Bro Its not decent way of asking a question.. Yes there are many good developers in this community who helps people like me and you.. and YES THEY KNOW !!

    anyways share the code so that everyone including me can have a look.
    Also have you set permissions in Manifest file??

  • hkiddhkidd Member ✭✭

    @Saraswati_Chandra Sorry - I am new to Xamarin and don't know how Forums works yet!

    I can't post links or photos because I am new to Xamarin unfortunately.

    I have set these permissions in the Manifest file:


    I have these assembly directions in the AssemblyInfo.cs file:
    [assembly: UsesPermission(Android.Manifest.Permission.Internet)]
    [assembly: UsesPermission(Android.Manifest.Permission.WriteExternalStorage)]
    [assembly: UsesPermission(Android.Manifest.Permission.ReadExternalStorage)]
    [assembly: UsesFeature("android.hardware.camera", Required = false)]
    [assembly: UsesFeature("android.hardware.camera.autofocus", Required = false)]

    I have the required method in my MainActivity file that @JamesMontemagno specifies in his git setup directions.

    In my MainPage.xaml file in the shared project, I have an image named photo and a button named CameraButton that should TakePhotoOnAsync() when pressed.

    In my MainPage.xaml.cs file in the shared project, I have the following code to implement the CameraButton functionality when the camera button is pressed:

    private async void CameraPressed(object sender, EventArgs e)
    {
    // If there is not a working, available camera, the button will appear disabled
    if (!Plugin.Media.CrossMedia.Current.IsCameraAvailable || !Plugin.Media.CrossMedia.Current.IsTakePhotoSupported)
    {
    await DisplayAlert("Camera not supported", "Camera not supported", "OK");
    CameraButton.IsEnabled = false;
    return;
    }

            // verifies that the user has permission to access the camera
            var cameraStatus = await CrossPermissions.Current.CheckPermissionStatusAsync(Permission.Camera);
            if (cameraStatus != PermissionStatus.Granted)
            {
                var results = await CrossPermissions.Current.RequestPermissionsAsync(new[] { Permission.Camera });
                cameraStatus = results[Permission.Camera];
                if (cameraStatus != PermissionStatus.Granted)
                {
                    await DisplayAlert("Permissions Denied", "Unable to take photos.", "OK");
                    CameraButton.IsEnabled = false;
                    return;
                }
            }
    
            // Assures user intends to take a photo
            await DisplayAlert("Take picture confirmation", "Would you like to take a picture?", "Yes");
    
            // Accesses Media Plugin's TakePhotoAsync method to take a the photo
            var photo = await Plugin.Media.CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions() {
                Directory = "Sample",
                Name = "test.jpg",
            });
    
            // Displays the image if their is image information
            if (photo != null)
            {
                Photo.Source = ImageSource.FromStream(() => { return photo.GetStream(); });
                Photo.Aspect = Aspect.AspectFill;
            }
        }
    

    The display alert "Permissions Denied" pops up every time. I haven't been able to get permission to get past this step. Is there some set up step that I have done incorrectly?

    Was this code too difficult to visualize?

  • Saraswati_ChandraSaraswati_Chandra USMember ✭✭✭

    I guess you are simply trying what is given in the documentation of media plugin, so it should work.
    Anyways would you mind to create the zip file of your app and share it here, so that I can run it myself.

    If not let me know, I will create a sample and upload it. I would be happy to help since you are a new xamarin developer :)

  • hkiddhkidd Member ✭✭
    edited June 2018

    I am unable to upload the zip file because I can't add any files as a new developer :(

    I would be happy to send it to you via email or some other platform - would that work for you?

    Thank you so much!! I am sure it is a simple thing that I have missed.

  • hkiddhkidd Member ✭✭

    Update: It is returning the PermissionStatus to be "Unknown"

  • hkiddhkidd Member ✭✭
    Accepted Answer

    Update: I just had to add a block of code that redirected the user to the OpenAppSettings() and then navigate back to the app and it gave me permission

  • arminmosaffaiearminmosaffaie Member ✭✭

    Can you guys look into my code (https://github.com/Arminmosaffaie/CameraTest)? i get the error: Uncaught exception cannot throw 'Plugin.MediaPermissionException' on thread...

  • suraj007suraj007 Member ✭✭

    Add the following to your AndroidManifest.xml inside the tag


  • suraj007suraj007 Member ✭✭
    <provider android:name="android.support.v4.content.FileProvider" android:authorities="com.AppName.app.fileprovider" android:exported="false" android:grantUriPermissions="true">
            <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/provider_paths"></meta-data>
        </provider>
    
  • suraj007suraj007 Member ✭✭

    Add a new folder called xml into your Resources folder and add a new XML file called provider_paths.xml

  • suraj007suraj007 Member ✭✭
    edited January 1

    Add a new folder called xml into your Resources folder and add a new XML file called provider_paths.xml

    <?xml version="1.0" encoding="utf-8"?>


  • suraj007suraj007 Member ✭✭

    Use this intent

    Intent intent = new Intent(MediaStore.ActionImageCapture);
    BitmapHelper.file = new File(BitmapHelper._dir, String.Format("myProfileTemp{0}.jpg", Guid.NewGuid()));

                Android.Net.Uri uri = FileProvider.GetUriForFile(this.BaseContext, "com.AppName.app.fileprovider", BitmapHelper._file);
                intent.PutExtra(MediaStore.ExtraOutput, uri);
    
                StartActivityForResult(intent, CAMERA_REQUEST);
    
Sign In or Register to comment.