Crop an image when selected from gallery in android

Solution 1:

Yes it's possible to crop image in android by using com.android.camera.action.CROP. after picking image url from gallery.you will start Crop Editor as:

Intent intent = new Intent("com.android.camera.action.CROP");  
intent.setClassName("com.android.camera", "com.android.camera.CropImage");  
File file = new File(filePath);  
Uri uri = Uri.fromFile(file);  
intent.setData(uri);  
intent.putExtra("crop", "true");  
intent.putExtra("aspectX", 1);  
intent.putExtra("aspectY", 1);  
intent.putExtra("outputX", 96);  
intent.putExtra("outputY", 96);  
intent.putExtra("noFaceDetection", true);  
intent.putExtra("return-data", true);                                  
startActivityForResult(intent, REQUEST_CROP_ICON);

When the picture select Activity return will be selected to save the contents.in onActivityResult:

Bundle extras = data.getExtras();  
if(extras != null ) {  
    Bitmap photo = extras.getParcelable("data");  
    ByteArrayOutputStream stream = new ByteArrayOutputStream();  
    photo.compress(Bitmap.CompressFormat.JPEG, 75, stream);  
        // The stream to write to a file or directly using the photo
}

and see this post which is also help you for cropping image in android

Solution 2:

This tutorial is exactly what you need enjoy:

Picking image from gallery:

enter image description here

Crop image after Intent pick action

enter image description here

Cheers

Solution 3:

You can already tell the Camera/Gallery-Intent to start the crop-editor after the image is selected/taken:

Pick Image from Gallery:

Intent pickImageIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

pickImageIntent.setType("image/*");
pickImageIntent.putExtra("crop", "true");
pickImageIntent.putExtra("outputX", 200);
pickImageIntent.putExtra("outputY", 200);
pickImageIntent.putExtra("aspectX", 1);
pickImageIntent.putExtra("aspectY", 1);
pickImageIntent.putExtra("scale", true);
pickImageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriWhereToStore);
pickImageIntent.putExtra("outputFormat",

Bitmap.CompressFormat.JPEG.toString());
startActivityForResult(pickImageIntent, RESULT_LOAD_IMAGE);

Take Photo:

Intent takePicIntent = new Intent("android.media.action.IMAGE_CAPTURE");

takePicIntent .putExtra("crop", "true");
takePicIntent .putExtra("outputX", 200);
takePicIntent .putExtra("outputY", 200);
takePicIntent .putExtra("aspectX", 1);
takePicIntent .putExtra("aspectY", 1);
takePicIntent .putExtra("scale", true);
takePicIntent .putExtra(MediaStore.EXTRA_OUTPUT, uriWhereToStore);
takePicIntent .putExtra("outputFormat",

Bitmap.CompressFormat.JPEG.toString());
startActivityForResult(takePicIntent , RESULT_LOAD_IMAGE);

Solution 4:

Although part of the internal API, the com.android.camera.action.CROP seems like it is well-supported on most Android devices. This might get you started:

final Intent intent = new Intent("com.android.camera.action.CROP");
intent.setData(uriOfImageToCrop);
intent.putExtra("outputX", 400);
intent.putExtra("outputY", 400);
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("scale", true);
intent.putExtra("noFaceDetection", true);
intent.putExtra("output", Uri.fromFile(someOutputFile));
startActivityForResult(intent, SOME_RANDOM_REQUEST_CODE);

Then handle what you need to do in the onActivityResult() method of your Activity. Your output file should be the cropped image.

Since this Intent action is part of the internal API, however, I would strongly advise that you have some sort of fallback behavior if some device does not support the Intent. Some manufacturers provide their own Gallery apps and so there is no way of knowing whether or not the user's device will recognize the Intent. PLEASE DON'T FORGET THIS!! :)