Many apps, such as Facebook, have this feature where after a user chooses an image from their device's gallery, the user is then brought to a preview where they can choose a cropped version of their image. After looking through SO (e.g. How to crop image on camera preview "Goggles style" - Android), I found this link to a an example, but the zip seems to be for sale (CameraPreview.zip
). Since that post is a bit old, I was wondering if someone is aware of another example out there or if they know how to do it, please share your knowledge. Thanks.
Asked
Active
Viewed 2,345 times
1

Community
- 1
- 1

Katedral Pillon
- 14,534
- 25
- 99
- 199
1 Answers
3
Try the below code, change the parameters as per your requirements:
Intent cropIntent = new Intent("com.android.camera.action.CROP");
//indicate image type and Uri
cropIntent.setDataAndType(fileUri, "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", 350);
cropIntent.putExtra("outputY", 350);
//retrieve data on return
//cropIntent.putExtra("return-data", true);
// some code to retriev an valid Uri
cropUri = Uri.fromFile(getOutputMediaFile(MEDIA_TYPE_IMAGE));
cropIntent.putExtra(MediaStore.EXTRA_OUTPUT, cropUri);
//start the activity - we handle returning in onActivityResult
startActivityForResult(cropIntent, CROP_REQ_CODE);
and you can get the output using in OnActivityResult()
Bundle extras = data.getExtras();
//get the cropped bitmap
clickedPhoto = extras.getParcelable("data");

Aakash
- 1,860
- 19
- 30
-
am I to replace the normal gallery intent code with the one you provide? I mean `Intent gallery = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); gallery.setType("image/*"); startActivityForResult(gallery, GALLERY_PHOTO_REQUEST_CODE);` – Katedral Pillon Apr 18 '14 at 04:21
-
1no after you get the result for GALLERY_PHOTO_REQUEST_CODE in OnActivityResult(). You need to pass the Uri to to the CROP intent. – Aakash Apr 18 '14 at 04:23
-
Is there a simple way to include rotation functionality as well? – Katedral Pillon Apr 18 '14 at 10:08