I am using "com.android.camera.action.CROP" to crop images from user`s image gallery..
Crop function:
private void performCrop(String picUri) {
try {
Intent cropIntent = new Intent("com.android.camera.action.CROP");
File f = new File(picUri);
Uri contentUri = Uri.fromFile(f);
cropIntent.setDataAndType(contentUri, "image/*");
cropIntent.putExtra("crop", "false");
cropIntent.putExtra("aspectX", 1);
cropIntent.putExtra("aspectY", 1);
cropIntent.putExtra("outputX", 1024); //512
cropIntent.putExtra("outputY", 1024); //512
cropIntent.putExtra("return-data", true);
startActivityForResult(cropIntent, RESULT_CROP);
}
catch (ActivityNotFoundException e) {
String errorMessage = "your device doesn't support the crop action!";
Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
toast.show();
}
}
and set the result to image view:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == GALLERY_ACTIVITY_CODE) {
if(resultCode == Activity.RESULT_OK){
picturePath = data.getStringExtra("picturePath");
//perform Crop on the Image Selected from Gallery
performCrop(picturePath);
}
}
if (requestCode == RESULT_CROP ) {
if(resultCode == Activity.RESULT_OK){
Bundle extras = data.getExtras();
Bitmap selectedBitmap = extras.getParcelable("data");
// Set The Bitmap Data To ImageView
addImageImageView.setImageBitmap(selectedBitmap);
addImageImageView.setScaleType(ImageView.ScaleType.FIT_XY);
}
}
}
On some devices, crop action returns a blurry and small size image (eg. a 4128*2322 pixel image turns to a 160*160 pixel cropped image). While on most devices, it works great. I am not sure what outputX and Y are really do, but changing them doesn`t solve my problem.