Android EasyPermissions拍照权限管理申请

1、在build.gradle中添加如下
//权限
implementation('pub.devrel:easypermissions:2.0.1') {
    exclude group: 'com.android.support'
}
2、在activity中使用
public void showCamera(int num) {
    String[] permissions = new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE};
    if (!EasyPermissions.hasPermissions(context, permissions)) {
        //弹出一个对话框进行提示用户
        PermissionUtils.requestPermissions(context, 100, permissions, null);
    }else {
        openCamera(num);
    }
}

//判断权限的结果码
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    
    if(requestCode == 100) {
        //如果已经允许该权限
        if(grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            openCamera(selectId);
        }else {
            //否则,提示该权限已经被禁止
            ToastUtil.shortshow(context,"未授予拍照权限");
        }
        return;
    }
}
上一篇:Android | 教你如何开发扫二维码功能


下一篇:Android6.0运行权限框架封装