图像裁剪许可后的onSuccessListner

时间:2018-10-14 04:24:53

标签: android imageview crop

我正在像这样裁剪图像:

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

                ;


            }

        }

我的问题是,在授予或拒绝权限后,我想立即进行一些活动。在当前情况下,我可以先授予权限,然后再次单击我正在使用的imageview,然后进行裁剪。但是我想在授予权限后立即进行一项活动,如果拒绝则进行一项活动。

2 个答案:

答案 0 :(得分:2)

您应该在活动中覆盖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 
                }
            } 
        }
    }
}

答案 1 :(得分:0)

@Override
public void onRequestPermissionsResult(int requestCode,
                                       String permissions[], int[] grantResults) {
    switch (requestCode) {
        case MY_PERMISSIONS_REQUEST_FOR_LOCATION: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // permission was granted, yay! Do the
                // contacts-related task you need to do.

            } else {

                // permission denied, boo! Disable the
                // functionality that depends on this permission.
            }
            return;
        }

        // other 'case' lines to check for other
        // permissions this app might request
    }
}

您可能需要在活动中覆盖它以处理请求的结果