VS Xamarin.Forms(Android):Xam.Plugin.Media拒绝对照相机和存储的访问 [英] VS Xamarin.Forms (Android): Camera and Storage access denied with Xam.Plugin.Media

查看:126
本文介绍了VS Xamarin.Forms(Android):Xam.Plugin.Media拒绝对照相机和存储的访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Xamarin.Forms应用程序,目前正在设备上对Android 10进行测试.我要有两个按钮:(从存储空间中)选择照片和(从相机中)拍摄照片.我没有权限访问存储设备和相机时遇到了一些问题,因此我遵循了本教程:

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.'

我确实向AndroidManifest.xml添加了权限:

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>

但仍然没有结果.

  • 在物理设备上调试:Samsung Galaxy S10,Android 10.
  • 所有软件包都是最新的.
  • Visual Studio 2019.

有人知道如何解决这个问题吗?

Does someone know how to solve this?

最诚挚的问候, 加内什

Best regards, Ganesh

推荐答案

您可以使用 Xamarin.Essentials.Platform.OnRequestPermissionsResult来请求权限.将其添加到OnRequestPermissionsResult方法.

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);
        }
    }

如果使用CrossMedia,则必须获取StorageWriteCamera权限.您可以创建一个PermissionUtils类来处理它.

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";
                }

此处正在运行GIF.

这是我的演示,您可以下载它并在android 10模拟器中进行测试.

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

https://github.com/851265601/XFormsCrossMediaPermission

这篇关于VS Xamarin.Forms(Android):Xam.Plugin.Media拒绝对照相机和存储的访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆