Android checks camera permissions in the target SDK 22 or earlier

I have this code to check the permissions of the camera

if (Build.VERSION.SDK_INT >= 23) {
Log.e("Permission State",ContextCompat.checkSelfPermission(activity, Manifest.permission.CAMERA)+"");

// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(activity, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED){
showCameraActivityForResult(activity);
}else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.CAMERA}, Constant.MY_PERMISSIONS_REQUEST_CAMERA);
}
}else{
showCameraActivityForResult(activity);
}

The problem is that I always have the permissions granted in version 5.1 or earlier. Other users also say here

How can I know if my app’s camera permission is granted?

In some devices, such as Samsung, users can disable camera permissions from device settings. Therefore, when users open my app and click the camera, it will always be blank. How to detect whether users can use the camera? (it must be different from my code because it doesn't work.)

resolvent:

If you use appcompatactivity, you can use checkselfpermission to check whether permissions have been granted

if (ContextCompat.checkSelfPermission(context, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
     if (ActivityCompat.shouldShowRequestPermissionRationale((Activity) context, Manifest.permission.CAMERA)) {

      //Show permission dialog
     } else {

        // No explanation needed, we can request the permission.
        ActivityCompat.requestPermissions((Activity)context, new String[]{Manifest.permission.CAMERA}, code);
    }
}

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>