简体   繁体   English

未经相机许可无法打开相机“0”

[英]cannot open camera "0" without camera permission

I have the following in my manifest我的清单中有以下内容

<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-permission android:name="android.permission.CAMERA" />

however I receive this error但是我收到此错误

An error occurred while connecting to camera 0: Status(-8, EX_SERVICE_SPECIFIC): '1: validateClientPermissionsLocked:1165: Caller ... (PID 10153, UID 6049) cannot open camera "0" without camera permission'

I am attempting to get a camera working using this code我正在尝试使用此代码让相机工作

public static Camera getCameraInstance(){
    Camera c = null;
    try {
        c = Camera.open();
    } catch (Exception e) {
        Log.e("getCameraInstance", "exception", e);
    }
    return c; // returns null if camera is unavailable
}

How do I get this camera working?如何让这台相机工作?

Need to enable permissions at runtime.需要在运行时启用权限。 The above error is outputted when the 0 indexed camera does not have permissions.当索引为0的相机没有权限时,输出上述错误。 Adding permissions to the manifest is not what enables it on the phone... the below code will.向清单添加权限并不是在手机上启用它的原因......下面的代码将。

    public static void checkCameraPermissions(Context context){
        if (ContextCompat.checkSelfPermission(context, Manifest.permission.CAMERA)
                != PackageManager.PERMISSION_GRANTED)
        {
            // Permission is not granted
            Log.d("checkCameraPermissions", "No Camera Permissions");
            ActivityCompat.requestPermissions((Activity) context,
                    new String[] { Manifest.permission.CAMERA },
                    100);
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM