android image preview example for cropping

强颜欢笑 提交于 2020-01-05 04:12:12

问题


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.


回答1:


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");


来源:https://stackoverflow.com/questions/23147254/android-image-preview-example-for-cropping

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!