VS Xamarin.Forms (Android): Camera and Storage access denied with Xam.Plugin.Media

对着背影说爱祢 提交于 2020-06-29 03:43:07

问题


I have an Xamarin.Forms app, which I test for Android 10 (on device) at the moment. I want to have two buttons: pick photo (from storage) and take photo (from camera). I had some problems with not having the permissions to access the storage and the camera, so I followed this tutorial: https://www.youtube.com/watch?v=IVvJX4CoLUY&t=0s&list=PLv-NAmQyi2iOu7zfbPDhLTXgBwUwVIBga&index=1 . This tutorial is everything I need, since these two buttons are discussed here. I'm using Xam.Plugin.Media for this project.

Also with this tutorial I'm having trouble with getting permissions to my camera and my storage. The error is constantly the same: 'Camera permission(s) are required.' or: 'Storage permission(s) are required.'

I did add the permissions to AndroidManifest.xml:

<?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.takepicturetest1">
    <uses-sdk android:minSdkVersion="21" android:targetSdkVersion="28" />
    <application android:label="TakePictureTest1.Android"></application>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.hardware.camera.autofocus" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
</manifest>

But still no results.

  • Debug on physical device: Samsung Galaxy S10, Android 10.
  • All packages are up-to-date.
  • Visual Studio 2019.

Does someone know how to solve this?

Best regards, Ganesh


回答1:


You can use Xamarin.Essentials.Platform.OnRequestPermissionsResult to request permissions. Add it in the OnRequestPermissionsResult method.

  [Activity(Label = "IssuesApp", Icon = "@mipmap/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
    {
        protected override void OnCreate(Bundle savedInstanceState)
        {
            TabLayoutResource = Resource.Layout.Tabbar;
            ToolbarResource = Resource.Layout.Toolbar;

            base.OnCreate(savedInstanceState);

            Xamarin.Essentials.Platform.Init(this, savedInstanceState);
            global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
            LoadApplication(new App());
        }

        public override  void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
        {
        
            Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);

            base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }

If you use CrossMedia You have to get the StorageWrite and Camera permissons. You can create a PermissionUtils class to handle it.

    public static class PermissionUtils
    {
        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;
        }
    }

Before you take the photo, you should check the permission, if you do get both of them, you should request it like following code.

 if (!await PermissionUtils.GetPermission<Permissions.Camera>())
                {
                    return "You do not have this permission";
                }

Here is running GIF.

Here is my demo, you can download it and test it in your android 10 emulator.

https://github.com/851265601/XFormsCrossMediaPermission




回答2:


You must also handle the runtime persmissions as documented here.

The WRITE_EXTERNAL_STORAGE & READ_EXTERNAL_STORAGE permissions are required, but the library will automatically add this for you. Additionally, if your users are running Marshmallow the Plugin will automatically prompt them for runtime permissions. You must add the Permission Plugin code into your Main or Base Activities:

Add to Activity:

public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Android.Content.PM.Permission[] grantResults)
{
    Plugin.Permissions.PermissionsImplementation.Current.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}


来源:https://stackoverflow.com/questions/60638673/vs-xamarin-forms-android-camera-and-storage-access-denied-with-xam-plugin-med

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!