locked
Camera and gallery are not working in android, showing permission exception RRS feed

  • Question

  • User351573 posted

    Getting the following exception when loading the camera and gallery in my project.

    Camera

    Exception:>Plugin.Media.Abstractions.MediaPermissionException: Camera permission(s) are required. at Plugin.Media.MediaImplementation+d__17.MoveNext () [0x0009

    Gallery

    Does not have storage permission granted, requesting.01-02 15:16:08.566 I/mono-stdout(12958): Does not have storage permission granted, requesting. Storage permission Denied. 01-02 15:16:08.567 I/mono-stdout(12958): Storage permission Denied.[0:] Exception:>Plugin.Media.Abstractions.MediaPermissionException: Storage permission(s) are required. at Plugin.Media.MediaImplementation+d__16.MoveNext () [0x00091] in <5939e997f291496f805023da28f3a447>:0 --- End of stack trace from previous location where exception was thrown ---

    I have added all the permissions in my manifest file.

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

    Added following codes in mainactivity:

    [assembly: UsesFeature("android.hardware.camera", Required = false)]
    [assembly: UsesFeature("android.hardware.camera.autofocus", Required = false)]
    
    public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Permission[] grantResults)
     {
         PermissionsImplementation.Current.OnRequestPermissionsResult(requestCode, permissions, grantResults);
     }
    

    Provider details in manifest file:

    <provider android:name="android.support.v4.content.FileProvider" android:authorities="{packagename}.fileprovider" android:exported="false" android:grantUriPermissions="true">
        <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths"></meta-data>
    </provider>
    

    file_paths.xml

    <?xml version="1.0" encoding="utf-8"?>
    <paths xmlns:android="http://schemas.android.com/apk/res/android">
        <external-files-path name="my_images" path="Pictures" />
        <external-files-path name="my_movies" path="Movies" />
    </paths>
    

    Camera and Gallery codes:

    public async void OpenMygallery()
        {
            try
            {
                await CrossMedia.Current.Initialize();
    
                if (!CrossMedia.Current.IsPickPhotoSupported)
                {
                    ShowAlert("No photos available.");
                    return;
                }
    
                _mediaFile = await CrossMedia.Current.PickPhotoAsync();
    
                if (_mediaFile == null)
                    return;
    
                tweetPicture.Source = ImageSource.FromStream(() =>
                {
                    return _mediaFile.GetStream();
                });
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("Exception:>" + ex);
            }
        }
    
    public async void OpenMyCamera()
        {
            try
            {
                await CrossMedia.Current.Initialize();
    
                if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported)
                {
                    ShowAlert("No camera available.");
                    return;
                }
    
                _mediaFile = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions
                {
                    Directory = "Sample",
                    Name = "test.jpg",
                    AllowCropping = true
                });
    
                if (_mediaFile == null)
                    return;
    
                tweetPicture.Source = ImageSource.FromStream(() =>
                {
                    return _mediaFile.GetStreamWithImageRotatedForExternalStorage();
                });
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("Exception:>" + ex);
            }
        }
    

    Bug Information:

    Version Number of Plugin: 4.0.1.5

    Device Tested On: Redmi Note 5 Pro

    Version of VS: 15.9.3

    Version of Xamarin: 3.4.0.1008975

    Anybody please suggest a solution for this?

    Wednesday, January 2, 2019 12:15 PM

Answers