Receive two requestCode == UCrop.REQUEST_CROP in onActivityResult?

560 Views Asked by At

I am using a uCrop library, to perform image cropping.

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

There are 1 best solutions below

1
On BEST ANSWER

You can create two class variables to identify which image is in cropping process.

private Uri mDestinationCapa;
private Uri mDestinationCamera;

 if (requestCode == CAMERA_REQUEST_CODE && resultCode == RESULT_OK) {
            mImageUri = data.getData();
            if (mImageUri == null) {
              ...
            } else {
                File tempCropped = new File(getCacheDir(), "tempImgCropped.png");
                mDestinationCamera= Uri.fromFile(tempCropped);
                ...
            }
        }

        if (requestCode == CAMERA_REQUEST_CODE_CAPA && resultCode == RESULT_OK) {
            mImageUri = data.getData();
            if (mImageUri == null) {
             ...
            } else {
                File tempCropped = new File(getCacheDir(), "tempImgCropped.png");
                mDestinationCapa = Uri.fromFile(tempCropped);
                ...
            }
        }

On ActivityResult you can compare the result Uri with the local Uris:

if (requestCode == UCrop.REQUEST_CROP) {

        final Uri imgUri = UCrop.getOutput(data);
        if(mDestinationCamera != null && imgUri.equals(mDestinationCamera)){
            //save camera cropped image 

        }if(mDestinationCamera != null && imgUri.equals(mDestinationCamera)){
            //save capa cropped image
        }
        return;
    }