Domanda

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.

È stato utile?

Soluzione

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");
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top