Take and Crop Image in Android

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

String destinationFileName= fileName;

Ucrop uCrop= Ucrop.of(uri, Uri.fromFile(new


File(Objects.requireNonNull(getActivity()).getCacheDir(), destinationFileName)));

Ucrop.Options options = new Ucrop.Options();

Options.setCompressionFormat(Bitmap.CompressorFormat.PNG);

Options.setHideBottomControls(false);

Options.setFreeStyleCropEnabled(true);

setupFragment(uCorp);

In the build.gradle file add the two dependency:

1) Com.android.support:design:27.1.0
2) Com.theartofdev.edmodo:android-image-cropper:2.6.0
3) Click on SyncNow
4) Now the permissions are required:
5) We are asking the user to provide the permissions at run tim:
6) For the external and internal storage of the device
7) Add the activity on the top of the default activity

https://www.androidlearning.com/capture-crop-image-device-camera/

https://www.androidlearning.com/capture-crop-image-device-camera/

1) How cropping happening


We’ll ask the user to provide the permission for camera and internal and external storage
After providing the permissions we’ll open the camera and the user will be allowed to take
the image here one activity will start to capture image and then for activity result we’ll get
the path of the Uri object
Once the image is captured we’ll be getting the image URI and the image name will be
decided based on the timestamp of the image taken in milli seconds
Here is a fun fact for you guys – whenevr we take image inany of your app when you’ll be
storing the name by default the image name will be set on some random numbers appended
by the string “Image_timestamp_file format” most of the times that random number is your
timestamp in milli seconds

Then we’ll create the Intent to crop an image and the following properties will be set
//call the standard crop action intent (the user device may not support it)

Intent cropIntent = new Intent("com.android.camera.action.CROP");

//indicate image type and Uri

cropIntent.setDataAndType(picUri, "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", 256);

cropIntent.putExtra("outputY", 256);

//retrieve data on return

cropIntent.putExtra("return-data", true);

//start the activity - we handle returning in onActivityResult

startActivityForResult(cropIntent, PIC_CROP);

Finally we can retrieve the cropped image and display it within the app UI.
Inside your “onActivityResult” method, after the “if” statement in which
you check for the “CAMERA_CAPTURE” request code, add an “if else”
statement checking for the “PIC_CROP” code, in which case we are
returning from the crop operation:

//user is returning from cropping the image

else if(requestCode == PIC_CROP){

Inside this statement, we can retrieve the returned cropped image as follows:

//get the returned data

Bundle extras = data.getExtras();

//get the cropped bitmap

Bitmap thePic = (Bitmap) extras.get("data");

We now have the user’s cropped image as a Bitmap. Let’s display it in the Image
View as follows:
//display the returned cropped image

imageView.setImageBitmap(thePic);

You might also like