我像这样进行图像裁剪:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
//do some activity based on permission granted or declined.
} else {
CropImage.activity(mainImageURI)
.setGuidelines(CropImageView.Guidelines.ON)
.setAspectRatio(1,1)
.setAutoZoomEnabled(false)
.start(this);
;
}
}
我的问题是我想在授予或拒绝权限后立即进行一些活动。在当前情况下,我可以先授予权限,然后再次单击我正在使用的图像视图,然后进行裁剪。但是我想在授予权限后立即进行一项活动,如果权限被拒绝,则进行一项活动。
您应该覆盖活动中的onRequestPermissionsResult
,以便在授予或未授予权限时收到通知:
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == PERMISSIONS_CODE) {
for (int i = 0; i < permissions.length; i++) {
String permission = permissions[i];
int grantResult = grantResults[i];
if (permission.equals(Manifest.permission.READ_EXTERNAL_STORAGE)) {
if (grantResult == PackageManager.PERMISSION_GRANTED) {
// TODO Open The Crop Activity
} else {
// TODO Tell the user your app can't function properly
}
}
}
}
}