Android crop image from camera or gallery

In some applications we can see that image picked using gallery or camera is taken to a crop screen before saving the it for limiting the size of the image. This can be achieved by using android default crop feature. In this tutorial we will learn how to crop an image from gallery or camera.

Please refer the tutorial How to pick image from gallery or camera if you are not familiar with camera functionalities.

Let us start by creating a layout with an ImageView to display cropped image and a button to open camera.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">


<ImageView
android:id="@+id/im_crop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"/>

<Button
android:id="@+id/btn_camera"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Camera"
android:layout_alignParentBottom="true" />

</RelativeLayout>

Now add the following code to your Activity’s class file

Note: Here i am using image from camera only. You can use the same crop function in the below code for image from gallery also.

public class Crop extends AppCompatActivity {

private Uri picUri;
File f;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera_crop);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

Button btn_camera = (Button) findViewById(R.id.btn_camera);
btn_camera.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

try {

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
f = new File(android.os.Environment.getExternalStorageDirectory(), "makegifimage.jpg");
picUri = Uri.fromFile(f);
intent.putExtra(MediaStore.EXTRA_OUTPUT, picUri);
startActivityForResult(intent, 1);

} catch (ActivityNotFoundException anfe) {

Toast.makeText(getApplicationContext(),"couldnt open your camera",Toast.LENGTH_SHORT).show();

}
}
});

}


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

if (resultCode == RESULT_OK) {
if (requestCode == 1) {

CropImage();

} else if (requestCode == 2) {

BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();

Bitmap bitmap = BitmapFactory.decodeFile(f.getAbsolutePath(),
bitmapOptions);
ImageView im_crop = (ImageView) findViewById(R.id.im_crop);
im_crop.setImageBitmap(bitmap);

}
}

}

private void CropImage() {
try {

Intent cropIntent = new Intent("com.android.camera.action.CROP");
cropIntent.setDataAndType(picUri, "image/*");
cropIntent.putExtra("crop", "true");
cropIntent.putExtra("aspectX", 2);
cropIntent.putExtra("aspectY", 2);
cropIntent.putExtra("outputX", 256);
cropIntent.putExtra("outputY", 256);
cropIntent.putExtra("return-data", true);
startActivityForResult(cropIntent, 2);
}
catch (ActivityNotFoundException e) {

Toast.makeText(this, "Your device is not supportting the crop action", Toast.LENGTH_SHORT);

}
}
}

Now add the following permissions to your AndroidManifest.xml

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />

If you are using targetversion>22 you need to have run time permissions. Please refer the below link to see how request the permissions at run time in android

How to request permissions dynamically at run time in android

About the author

Hi guys, i am the author of codesfor. I am a B.Tech graduate currently working as an App developer. Apart from job i am a blogger and a freelancer.

Add a Comment

Your email address will not be published. Required fields are marked *