1.用户已经同意该权限
2.用户拒绝了该权限,没有选中『不再询问』(Never ask again),那么下次再次启动时,还会提示请求权限的对话框
3.用户拒绝了该权限,并且选中『不再询问』,提醒用户手动打开权限
所以第三种,就是没有权限,也不会给你弹出系统权限
//相机
public void getphoto() {
PictureDialog pictureDialog = new PictureDialog();
pictureDialog.setOnPersonalListener(new PictureDialog.OnPersonalListener() {
@Override
public void onTop() {
File file = new File(Environment.getExternalStorageDirectory().getPath() + "/" + System.currentTimeMillis() + ".jpg");
imageuri = Uri.fromFile(file);
//用于判断SDK版本是否大于23
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
//检查权限
int i = ContextCompat.checkSelfPermission(getContext(), PERMISSIONS_STORAGE[0]);//0表示读写权限
int i2 = ContextCompat.checkSelfPermission(getContext(), PERMISSIONS_STORAGE[1]);//1表示照相权限
//如果权限申请失败,则重新申请权限
if (i == PackageManager.PERMISSION_DENIED && i2 == PackageManager.PERMISSION_DENIED) {//-1没授权
//重新申请权限函数
startRequestPermission();
Logger.e("权限请求成功");
} else if (i == PackageManager.PERMISSION_GRANTED && i2 == PackageManager.PERMISSION_GRANTED) {//0授权
PictureUtils.takePicture(CameraEvoActivity.this, imageuri, 4001);
} else {
HintTitleDialog myDialog = new HintTitleDialog("需要到系统中设置相关权限");
myDialog.show(getSupportFragmentManager());
myDialog.setOnLeftClick(new HintDialog.OnLeftClick() {
@Override
public void onLeft() {
ToastUtils.show("取消");
}
});
myDialog.setOnRightClick(new HintDialog.OnRightClick() {
@Override
public void onRight() {
myDialog.dismiss();
startAppDetailSetting(CameraEvoActivity.this, 101);
}
});
}
} else {
PictureUtils.takePicture(CameraEvoActivity.this, imageuri, 4001);
}
}
@Override
public void onBottom() {
Matisse.from(CameraEvoActivity.this)
.choose(MimeType.of(MimeType.JPEG, MimeType.PNG)) // 选择 mime 的类型
.theme(R.style.Matisse_Dracula)//Zhihu(亮蓝色主题) Dracula(黑色主题)
.countable(true)
.maxSelectable(1) // 图片选择的最多数量
.spanCount(3)//网格的规格
.restrictOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED)//图像选择和预览活动所需的方向。
.thumbnailScale(0.85f) // 缩略图的比例
.imageEngine(new GlideEngine()) // 使用的图片加载引擎
.forResult(5001); // 设置作为标记的请求码
}
}).show(getSupportFragmentManager());
}
/**
* 获取应用详情页面intent
*/
public static void startAppDetailSetting(Activity context, int request) {
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", context.getPackageName(), null);
intent.setData(uri);
context.startActivityForResult(intent, request);
}
//重新申请权限函数
private void startRequestPermission() {
//4001为请求码
ActivityCompat.requestPermissions(this, PERMISSIONS_STORAGE, 4001);
}
//请求权限时的回调方法
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
//检查权限
int i = ContextCompat.checkSelfPermission(getContext(), PERMISSIONS_STORAGE[0]);
int i2 = ContextCompat.checkSelfPermission(getContext(), PERMISSIONS_STORAGE[1]);
//如果权限申请失败,则重新申请权限
if (i == PackageManager.PERMISSION_DENIED && i2 == PackageManager.PERMISSION_DENIED) {//-1没授权
//重新申请权限函数
startRequestPermission();
Logger.e("权限请求成功");
} else if (i == PackageManager.PERMISSION_GRANTED && i2 == PackageManager.PERMISSION_GRANTED) {//0授权
PictureUtils.takePicture(CameraEvoActivity.this, imageuri, 4001);
} else {
HintTitleDialog myDialog = new HintTitleDialog("需要到系统中设置相关权限");
myDialog.show(getSupportFragmentManager());
myDialog.setOnLeftClick(new HintDialog.OnLeftClick() {
@Override
public void onLeft() {
ToastUtils.show("取消");
}
});
myDialog.setOnRightClick(new HintDialog.OnRightClick() {
@Override
public void onRight() {
myDialog.dismiss();
startAppDetailSetting(CameraEvoActivity.this, 101);
}
});
}
}
//回传值
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 4001 && resultCode == RESULT_OK) {
imgPhotoNo.setVisibility(View.GONE);
imgPhoto.setVisibility(View.VISIBLE);
ivDeleteMed.setVisibility(View.VISIBLE);
headUri = getRealFilePath(imageuri);
GlideUtil.load(getContext(), headUri, imgPhoto);
} else if (requestCode == 5001 && resultCode == RESULT_OK) {
imgPhotoNo.setVisibility(View.GONE);
imgPhoto.setVisibility(View.VISIBLE);
ivDeleteMed.setVisibility(View.VISIBLE);
if (Matisse.obtainPathResult(data).size() != 0 && Matisse.obtainPathResult(data) != null) {
headUri = Matisse.obtainPathResult(data).get(0);
}
GlideUtil.load(getContext(), headUri, imgPhoto);
}
}
/**
* 获取uri真实路径
*/
public static String getRealFilePath(final Uri uri) {
String path = uri.getPath();
String data = path.replace("external_files", Environment.getExternalStorageDirectory().getPath());
Logger.d("获取uri真实路径: \nuri:" + path + "\npath:" + data);
return data;
}
GitHub网址:https://github.com/Blankj/AndroidUtilCode/blob/master/lib/utilcode/README-CN.md
在引导页加入:
//判断是否授权 phone电话权限 STORAGE读写权限 CAMERA照相权限
if (!PermissionUtils.isGranted(PermissionConstants.PHONE, PermissionConstants.STORAGE, PermissionConstants.CAMERA)) {
//设置请求权限
PermissionUtils.permission(PermissionConstants.PHONE, PermissionConstants.STORAGE, PermissionConstants.CAMERA).callback(new PermissionUtils.SimpleCallback() {
@Override
public void onGranted() {
Log.d("WelcomeActivity", "权限已申请");
}
@Override
public void onDenied() {
AppUtils.exitApp();
}
}).request();
}else{
//进行相关操作
}
//权限初始化 MultiDex.install(context);