其他分享
首页 > 其他分享> > Android EasyPermissions拍照权限管理申请

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

标签:拍照,EasyPermissions,int,grantResults,String,requestCode,Android,权限,permissions
来源: https://blog.csdn.net/wxx314165038/article/details/110312848