开发中,碰到6.0换头像获取不到权限问题,通过查找,找到以下解决方式:
在点击事件处判断当前版本是否大于等于6.0版本
final public static int REQUEST_CODE_ASK_CALL_PHONE = 123;
final public static int REQUEST_CODE_ASK_WRITE_STORGE = 124;
if (Build.VERSION.SDK_INT >= 23) {
int checkCallPhonePermission = ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA);
if (checkCallPhonePermission != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, REQUEST_CODE_ASK_CALL_PHONE);
return;
} else {
int checkCallPhonePermission2 = ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (checkCallPhonePermission2 != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_CODE_ASK_CALL_STORGE);
return;
} else {
Intent intent = new Intent();
intent.setClass(this, ChoosePhotoActivity.class);
startActivityForResult(intent, Code.REQUEST_HEAD_CODE);
}
}
} else {
Intent intent = new Intent();
intent.setClass(this, ChoosePhotoActivity.class);
startActivityForResult(intent, Code.REQUEST_HEAD_CODE);
}
然后在提示框返回结果中做操作,在activity中重写onRequestPermissionsResult方法
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case REQUEST_CODE_ASK_CALL_PHONE:
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Permission Granted
int checkCallPhonePermission2 = ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (checkCallPhonePermission2 != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_CODE_ASK_CALL_STORGE);
return;
} else {
Intent intent = new Intent();
intent.setClass(this, ChoosePhotoActivity.class);
startActivityForResult(intent, Code.REQUEST_HEAD_CODE);
}
} else {
// Permission Denied
Toast.makeText(BasicInformationActivity.this, "您禁止了相机权限", Toast.LENGTH_SHORT)
.show();
}
break;
case REQUEST_CODE_ASK_CALL_STORGE:
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Permission Granted
Intent intent = new Intent();
intent.setClass(this, ChoosePhotoActivity.class);
startActivityForResult(intent, Code.REQUEST_HEAD_CODE);
} else {
// Permission Denied
Toast.makeText(BasicInformationActivity.this, "您禁止了写入权限", Toast.LENGTH_SHORT)
.show();
}
break;
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
希望能帮助遇到同样问题的你