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.CROP
for testing purposes and call in the catch another method with another resultCode NO_CROP
but always the actual resultCode PICK_CROP
is called also.
How can I ignore PICK_CROP
in 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);
}
}