MediaPlugin: Plugin.Media.Abstractions.MediaPermissionException

When I debug my app in Android API 19 it works but when I debug in Android API 24 the exception “Plugin.Media.Abstractions.MediaPermissionException: Camera permission (s) is required” is displayed when I want to access the camera and this exception comes out when I want to select a photo from the gallery “Plugin.Media.Abstractions.MediaPermissionException: Storage permission (s) are required”.

Bug Information

Version Number of Plugin: 3.1.3 Device Tested On: Samsung Galaxy S7 (API 24), Samsung Galaxy Tab E (API 19) Simulator Tested On: N/A Version of VS: 15.6.4 Version of Xamarin: 4.9.0.752 Versions of other things you are using: Xam.Plugin.Connectivity 3.1.1, Xam.Plugins.Settings 3.1.1

Steps to reproduce the Behavior

C#

private ImageSource imageSource;
private MediaFile file;

private async void prueba(object sender, EventArgs args)
        {
            await CrossMedia.Current.Initialize();

            if (CrossMedia.Current.IsCameraAvailable && CrossMedia.Current.IsTakePhotoSupported)
            {
                var source = await DisplayActionSheet("¿Donde tomar la imagen?", "Cancelar", null, "Galeria", "Camara");

                switch (source)
                {
                    case "Cancelar":
                        file = null;
                        break;
                    case "Galeria":
                        file = await CrossMedia.Current.PickPhotoAsync(new PickMediaOptions
                        {
                            PhotoSize = PhotoSize.Small
                        });
                        break;
                    case "Camara":
                        file = await CrossMedia.Current.TakePhotoAsync(new StoreCameraMediaOptions
                        {
                            Name = "prueba.jpeg",
                            PhotoSize = PhotoSize.Small,
                            SaveToAlbum = true
                        });
                        break;
                    default:
                        break;
                }
            }
            else
            {
                file = await CrossMedia.Current.PickPhotoAsync(new PickMediaOptions
                {
                    PhotoSize = PhotoSize.Small
                });
            }

            if (file != null)
            {
                imageSource = ImageSource.FromStream(() =>
                {
                    var stream = file.GetStream();
                    file.Dispose();
                    return stream;
                });

                imageProduct.Source = imageSource;
            }
        }

AndroidManifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.companyname.Shopm" android:installLocation="auto">
	<uses-sdk android:minSdkVersion="15" android:targetSdkVersion="27" />
	<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
	<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
	<uses-permission android:name="android.permission.CAMERA" />
	<uses-permission android:name="android.permission.FLASHLIGHT" />
	<uses-permission android:name="android.permission.INTERNET" />
	<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
	<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
	<application android:label="Shopm">
    <provider android:name="android.support.v4.content.FileProvider"
          android:authorities="${applicationId}.fileprovider"
          android:exported="false"
          android:grantUriPermissions="true">

      <meta-data android:name="android.support.FILE_PROVIDER_PATHS"
                       android:resource="@xml/file_paths"></meta-data>
    </provider>
  </application>
</manifest>

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 17 (2 by maintainers)

Most upvoted comments

Actually it was my bad. When you install the plugin in Visual Studio it comes up with a comprehensive readme file within VS - once I had faithfully applied all the relevant advice it worked. I think the answer for me was CrossCurrentActivity.Current.Init(this, bundle);

It’s been a while since I solved the problem but I do not remember how I solved it. I remember that I installed a plugin called Permissions Plugin for Xamarin and followed the steps indicated by James. In the file MainActivity.cs of the Android project modify the following line:

Plugin.CurrentActivity.CrossCurrentActivity.Current.Activity.Init(this, bundle);

for this:

Plugin.CurrentActivity.CrossCurrentActivity.Current.Activity = this;

Because I produced compilation errors by other James plugins. Finally I was uploading the minimum API version to 19 (I recommend programming from API 21).

Another factor that is possible is that when compiling in Debug mode, you should put in Release mode when compiling, because in my case when compiling in Debug mode it did not show the window to accept permissions. (Check)

It’s been a while since I solved the problem but I do not remember how I solved it. I remember that I installed a plugin called Permissions Plugin for Xamarin and followed the steps indicated by James. In the file MainActivity.cs of the Android project modify the following line:

Plugin.CurrentActivity.CrossCurrentActivity.Current.Activity.Init(this, bundle);

for this:

Plugin.CurrentActivity.CrossCurrentActivity.Current.Activity = this;

Because I produced compilation errors by other James plugins. Finally I was uploading the minimum API version to 19 (I recommend programming from API 21).

Thanks for this, It started working all of a sudden. I did move the whole solution to another location and added it to git. Maybe there was something weird with the files

It’s been a while since I solved the problem but I do not remember how I solved it. I remember that I installed a plugin called Permissions Plugin for Xamarin and followed the steps indicated by James. In the file MainActivity.cs of the Android project modify the following line:

Plugin.CurrentActivity.CrossCurrentActivity.Current.Activity.Init(this, bundle);

for this:

Plugin.CurrentActivity.CrossCurrentActivity.Current.Activity = this;

Because I produced compilation errors by other James plugins. Finally I was uploading the minimum API version to 19 (I recommend programming from API 21).

This worked for me

Hi, After adding the required Permissions in Android for CAMERA, Read/Write External Storage, either in AndroidManifest.xml or at Android Project>>Properties>>Android Manifest>>Required Permissions; then make sure to enable the permission from your Device/Emulator itself: go to your Device or Emulator then Sittings>>search for Permission>> App Permissions>> Camera>>then Enable your deployed App to use Camera. Appreciated…