Yalantis / uCrop

Image Cropping Library for Android
https://yalantis.com/blog/introducing-ucrop-our-own-image-cropping-library-for-android/
11.86k stars 2.16k forks source link

Receive two requestCode == UCrop.REQUEST_CROP in onActivityResult? #377

Closed tiagobuchanelli closed 6 years ago

tiagobuchanelli commented 6 years ago

I need to cut two different images in the same activity. But with the code I have, the return is falling into a single loop.

How could I separate this return, to treat it independently?

Code:

 @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == RESULT_OK) {

            if (requestCode == UCrop.RESULT_ERROR) {
                Toast.makeText(this, "uCrop error", Toast.LENGTH_SHORT).show();
                return;
            }

            if (requestCode == UCrop.REQUEST_CROP) {

                mSelectImage.setImageResource(0);
                final Uri imgUri = UCrop.getOutput(data);
                mResultUri = imgUri;
                mSelectImage.setImageURI(mResultUri);
                temDadosSalvar = true;

                Toast.makeText(this, "Cortada", Toast.LENGTH_SHORT).show();
                return;
            }

            if (requestCode == CAMERA_REQUEST_CODE && resultCode == RESULT_OK) {

                mImageUri = data.getData();
                if (mImageUri == null) {
                    mProgress.dismiss();
                    return;
                } else {
                    File tempCropped = new File(getCacheDir(), "tempImgCropped.png");
                    Uri destinationUri = Uri.fromFile(tempCropped);
                    UCrop uCrop = UCrop.of(mImageUri, destinationUri);

                    uCrop = advancedConfig(uCrop);
                    uCrop.start(this);
                }
            }

            if (requestCode == CAMERA_REQUEST_CODE_CAPA && resultCode == RESULT_OK) {

                mImageUri = data.getData();
                if (mImageUri == null) {
                    mProgress.dismiss();
                    return;
                } else {
                    File tempCropped = new File(getCacheDir(), "tempImgCropped.png");
                    Uri destinationUri = Uri.fromFile(tempCropped);
                    UCrop uCrop = UCrop.of(mImageUri, destinationUri);

                    uCrop = advancedConfig(uCrop);
                    uCrop.start(this);
                }
            }
        }
    }
mderis commented 6 years ago

Hey Instead of uCrop.start(this); you can use uCrop.start(this, myRequestCode);

myRequestCodemight be any CONSTANT int to make difference between your different requests!