Android获取相机动态权限

1.

//使用兼容库就无需判断系统版本

val hasWriteStoragePermission: Int =

ContextCompat.checkSelfPermission(application, Manifest.permission.CAMERA)

if (hasWriteStoragePermission == PackageManager.PERMISSION_GRANTED) {

    //拥有权限,执行操作

    }else {

       //没有权限,向用户请求权限

    ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.CAMERA), 1)

}

2.

override fun onRequestPermissionsResult(requestCode: Int, permissions: Array, grantResults: IntArray) {

super.onRequestPermissionsResult(requestCode, permissions, grantResults)

        //通过requestCode来识别是否同一个请求

        if (requestCode ==1) {

            if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                // 获取到权限

            }else{                

                    if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA)) {

                         //用户不同意,向用户展示该权限作用

                    }else{

                        //用户j禁止不在弹出

           }

    }

}

你可能感兴趣的:(Android获取相机动态权限)