PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, July 27, 2022

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

 July 27, 2022     android, crop, imageview, java     No comments   

Issue

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

Solution

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



Answered By - Vibin
Answer Checked By - Cary Denson (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing