26

I want to crop an image in my application when it is selected from gallery. i.e if I launch the gallery and select an image the cropping window should come like when we select an image from iPhone. Is it possible in Android.

I found one tutorial for cropping the image in android, but dont seem the way I wanted.

http://www.coderzheaven.com/2011/03/15/crop-an-image-in-android/

user1293519
  • 1,593
  • 3
  • 16
  • 13
  • check [this](http://stackoverflow.com/questions/2085003/how-to-select-and-crop-an-image-in-android) i hope it will help you. – Simon Dorociak May 27 '12 at 18:57
  • see my answer hope that's what you are looking for, if you find some problem or you don't understand the code, let me know – K_Anas May 27 '12 at 19:38
  • I have posted my solution [here] on stackoverflow post (https://stackoverflow.com/a/44089387/1448357) – Gaurav May 24 '17 at 17:21

6 Answers6

27

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

miladsolgi
  • 430
  • 5
  • 19
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
  • 18
    The `com.android.camera.action.CROP` action is part of the Internal API and thus won't work in all cases. Just wanted to mention that :) – Alex Lockwood May 27 '12 at 20:15
  • In which cases isn't it gonna work ? When manufacturer has a legacy layer ? – Snicolas Mar 11 '13 at 12:57
  • 1
    This will not work on many, many devices. Android does not have a `CROP` `Intent`: http://commonsware.com/blog/2013/01/23/no-android-does-not-have-crop-intent.html – CommonsWare Jul 14 '14 at 10:39
  • how do i set minimum scale value ? For i.e. I want user to keep width and height at least of 300*300. – Jeeten Parmar Apr 25 '15 at 11:25
  • 3
    In my case, `data.getExtras()` returns a `Bundle` of `null`, then how could I get the target `Bitmap`? – SilentKnight Jun 12 '15 at 06:56
  • 3
    Doesn't work in 5.0+ with Photos app (Gallery was removed in Nexus devices after 5.0) – silent_grave Jun 17 '16 at 15:52
  • @AlexLockwood then what to use to do croping? and also `com.android.camera.action.CROP` works perfect in all device , only in lenovo not worked. it display toast that can't edit image under 50x50 ! what sholud to do? – Dharmishtha Jul 11 '17 at 06:37
  • @Dharmishtha Use a library or implement it yourself. Like I said, the intent is part of an internal API, so it is not guaranteed to work and it is not guaranteed to continue to work in the future. – Alex Lockwood Jul 12 '17 at 05:07
  • ok@AlexLockwood i have used library for cropping . the library is [Yalantis uCrop](https://github.com/Yalantis/uCrop) .And it works perfect. – Dharmishtha Jul 12 '17 at 06:30
  • What does `putExtra("return-data", true)` do? – HB. Mar 27 '20 at 05:32
21

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

K_Anas
  • 31,226
  • 9
  • 68
  • 81
17

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);
Till - Appviewer.io
  • 4,529
  • 1
  • 31
  • 35
15

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!! :)

Alex Lockwood
  • 83,063
  • 39
  • 206
  • 250
0

I solved this problem this way

private void pickUserImage() { 

if (doHavePermission()) { 
    Intent photoPickerIntent = new Intent(Intent.ACTION_PICK,
            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    photoPickerIntent.setType("image/*");
    photoPickerIntent.putExtra("crop", "true");
    photoPickerIntent.putExtra("scale", true);
    photoPickerIntent.putExtra("outputX", 256);
    photoPickerIntent.putExtra("outputY", 256);
    photoPickerIntent.putExtra("aspectX", 1);
    photoPickerIntent.putExtra("aspectY", 1);
    photoPickerIntent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
    photoPickerIntent.putExtra(MediaStore.EXTRA_OUTPUT, getTempUri());
    startActivityForResult(photoPickerIntent, PICK_FROM_GALLERY);
    } 
}

find my complete solution here in stackoverflow post

Gaurav
  • 1,965
  • 2
  • 16
  • 32
0

Nobody will tell you which extra is needed unless you try it:

   val intent = Intent(Intent.ACTION_PICK)
   intent.apply {
       type = "image/*"
       putExtra("crop", "true") // NOTE: should be string
       putExtra("outputX", 300) // This is needed, editor can't close without these two
       putExtra("outputY", 300) // This is needed

       putExtra("scale", true)
       putExtra("aspectX", 1)
       putExtra("aspectY", 1)
       putExtra("return-data", true)
   }
   startActivityForResult(intent, YOUR_INT_CODE)
BollMose
  • 3,002
  • 4
  • 32
  • 41