Skip to content
Advertisement

How to crop two different images in the same activity with different parameters in Android?

I am using this Crop library by SoundCloud. Selecting one imageview, slecting image, cropping and showing the result in the imageview works fine. But now I am trying to do it with two different imageviews with different specifications. I am not getting any errors nor am I seeing any results. Here’s what I have tried:

//My click listeners
regCoverPhoto.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Crop.pickImage(getActivity(), EditProfileDialog.this, REQUEST_CODE_COVER);
    }
});
regUserProfile.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Crop.pickImage(getActivity(), EditProfileDialog.this, REQUEST_CODE_PROFILE);
    }
});
//Handling the result
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_CODE_PROFILE && resultCode == Activity.RESULT_OK) {
        beginCropProfile(data.getData());
    }else if(requestCode == REQUEST_CODE_COVER && resultCode == Activity.RESULT_OK){
        beginCropCover(data.getData());
    } else if (requestCode == Crop.REQUEST_CROP) {
        handleCrop(requestCode, resultCode, data);
    }
}

private void beginCropProfile(Uri source) {
    Uri destination = Uri.fromFile(new File(getActivity().getCacheDir(), "cropped"));
    Crop.of(source, destination).withAspect(ASPECT_X, ASPECT_Y).start(getActivity(), EditProfileDialog.this, REQUEST_CODE_COVER);
}

private void beginCropCover(Uri source) {
    Uri destination = Uri.fromFile(new File(getActivity().getCacheDir(), "cropped"));
    Crop.of(source, destination).asSquare().start(getActivity(), EditProfileDialog.this, REQUEST_CODE_PROFILE);
}

private void handleCrop(int requestCode, int resultCode, Intent result) {
    if (requestCode == REQUEST_CODE_COVER && resultCode == Activity.RESULT_OK) {
        regCoverPhoto.setImageURI(Crop.getOutput(result));

        mCoverPhotoUri = Crop.getOutput(result);
        uploadCoverToStorage();

        Log.d(TAG,"ResultCover: " + Crop.getOutput(result).toString());
    }else if(requestCode == REQUEST_CODE_PROFILE && resultCode == Activity.RESULT_OK){
        regUserProfile.setImageURI(Crop.getOutput(result));
        mProfilePhotoUri = Crop.getOutput(result);
        uploadProfileToStorage();
        Log.d(TAG,"ResultProfile: " + Crop.getOutput(result).toString());
    } else if (resultCode == Crop.RESULT_ERROR) {
        Snackbar.make(getView(), Crop.getError(result).getMessage(), Snackbar.LENGTH_LONG).show();
    }
}

Advertisement

Answer

I haven’t used this library particularly, but custom request codes seem to be the problem.

Use different request codes for picking and cropping (total of 4 request codes) as you will need to handle them differently, and update onActivityResult(), handleCrop() to reflect that.

See https://gist.github.com/vibinr/fcf54c5e7ab63b9184432cc44c9a1494

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement