jdamcd / android-crop

Android library project for cropping images
4.54k stars 1.08k forks source link

How to have two different crop actions within the same activity? #264

Open power7714 opened 6 years ago

power7714 commented 6 years ago

I have two different ImageViews. One takes a square image and the other takes a specified ratio image. I'm trying to make it to where the user can click each image and crop them based on the specified parameters. This is what I have tried.

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

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

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

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

@jdamcd