1

I want to crop an image and call com.android.camera.action.CROP. As this doesn't exist on all devices I want to have a fallback to catch this and simply add the image w/o cropping.

To do so I rename com.android.camera.action.CROP to xcom.android.camera.action.CROPfor testing purposes and call in the catch another method with another resultCode NO_CROPbut always the actual resultCode PICK_CROPis called also.

How can I ignore PICK_CROPin this case so that it doesn't jump to drawImage()always where data is of course null and avoid a null poiner exception and instead go to noCrop()?

Here's my code:

//Start cropping intent
private void performCrop(int requestCode, Intent data) {

    // take care of exceptions
    try {
        // call the standard crop action intent (the user device may not
        // support it)
        Intent cropIntent = new Intent("xcom.android.camera.action.CROP");
        // indicate image type and Uri
        cropIntent.setDataAndType(imgUri, "image/*");
        // set crop properties
        cropIntent.putExtra("crop", "true");
        // indicate aspect of desired crop
        cropIntent.putExtra("aspectX", 1);
        cropIntent.putExtra("aspectY", 1);
        // indicate output X and Y
        cropIntent.putExtra("outputX", 256);
        cropIntent.putExtra("outputY", 256);
        // retrieve data on return
        cropIntent.putExtra("return-data", true);
        // start the activity - we handle returning in onActivityResult
        startActivityForResult(cropIntent, PIC_CROP);
    }
        // respond to users whose devices do not support the crop action
        catch (ActivityNotFoundException anfe) {
            // add chosen photo directly to imageview
            Bundle extras = data.getExtras();
            Bitmap photo = extras.getParcelable("data");
            noCrop(photo, NO_CROP, requestCode, data);
        }
    }

// draw image in circle canvas w/o crop -> fallback if crop doesn't exist on users device
private void noCrop(Bitmap photo, int requestCode, int resultCode, Intent data) {
    if (requestCode == NO_CROP) {
        ImageView profilepic = (ImageView) findViewById(R.id.profilepic);
        GraphicsUtil graphicUtil = new GraphicsUtil();
        profilepic.setImageBitmap(graphicUtil.getCircleBitmap(photo, 16));
        saveProfile(photo);
    } else {
        super.onActivityResult(requestCode, resultCode, data);
        }   
}

// Draw cropped image to imageview with circle canvas
private void drawImage(int requestCode, int resultCode, Intent data) {
    if (requestCode == PIC_CROP) {
        Bundle extras = data.getExtras();
        ImageView profilepic = (ImageView) findViewById(R.id.profilepic);
        Bitmap photo = extras.getParcelable("data");
        // display the returned cropped image
        GraphicsUtil graphicUtil = new GraphicsUtil();
        profilepic.setImageBitmap(graphicUtil.getCircleBitmap(photo, 16));
        saveProfile(photo);
    } else {
        super.onActivityResult(requestCode, resultCode, data);
    }   
}
indivisible
  • 4,892
  • 4
  • 31
  • 50
sascha
  • 265
  • 2
  • 4
  • 13

2 Answers2

0

As you said com.android.camera.action.CROP intent is not supported in all devices so why use it and write a fallback mechanism. There are many libraries available for cropping images. I have used Cropper and simple-image-crop library from github. Have a look at my answer here about simple-image-crop library.

Community
  • 1
  • 1
Illegal Argument
  • 10,090
  • 2
  • 44
  • 61
  • Thanks a lot for your feedback but as I'm a newby I struggle getting the gradle library into eclipse – sascha Jun 18 '14 at 17:01
  • that library is not in gradle or is it? Atleast it was not when I used it – Illegal Argument Jun 18 '14 at 17:02
  • At least it's missing the .project file and I don't get it managed to import it, even if I create a .project file. I get a strange message `/simple-crop-image-lib/gen already exists but is not a source folder. Convert to a source folder or rename it.`. At least it'S for me strange ;-) – sascha Jun 18 '14 at 17:28
  • are you using android studio?? I am an ecllipse user I convert all project to ecllipse before using them if they are from gradle – Illegal Argument Jun 18 '14 at 17:29
  • To solve the gen folder issue I found out that I had to add the gen folder as source for the cropimage lib project - so far so good. – sascha Jun 18 '14 at 19:59
  • but when I want to import `import eu.janmuller.android.simplecropimage.CropImage;`, Eclipse says that this can't be resolced though I have added it to the projects manifest - any idea? – sascha Jun 18 '14 at 20:01
  • anyway, I'm still curious why my actual problem exists and why it's not working as I expected it to work - where is the issue? – sascha Jun 18 '14 at 20:01
  • Additional Info: I got it working! To fiy the issue I had to create a new project and use the existing cropping library code intead of importing the library. That did the trick in case anybody has the same issues – sascha Jun 19 '14 at 14:23
  • @sascha I managed to do it without doing what you did. but anyways glad you could do it. If it works for you mind accepting my answer. – Illegal Argument Jun 19 '14 at 14:30
  • Of course :-) One last question: Does face detection work for you? In my current implementation it doesn't detect faces. Do I have to set any putExtra to enable it? – sascha Jun 19 '14 at 20:43
0

OK, I did a mistake. It's actually working to catch if the intent is not available. After some deeper debugging I found out that it crashes at Bitmap photo = extras.getParcelable("data"); because I'm not passing the correct data :-(

Sorry for any inconvenience!

sascha
  • 265
  • 2
  • 4
  • 13