Cropping an image in ANDROID programmatically has always been a problem for ANDROID developers.
So Here I am putting a sample code to demonstrate this.
For cropping an image in ANDROID we need a source bitmap which is our image itself, the other one is the target bitmap which we have to
set the size as we want and a matrix.

Below code is a fully working code, just copy and paste this code to your new project.
The XML for the code is also provided below.

Make sure that you have an image named “image.png” in your resources.
Assuming the image to be of size 300×300 pixels.

package Move3.pack;

import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.AbsoluteLayout;
import android.widget.FrameLayout;

@SuppressWarnings({ "deprecation", "unused" })
public class Move extends Activity {

	public FrameLayout board;
	public View part1;

	public void onCreate(Bundle savedInstanceState)
		 board = new FrameLayout(this);
		 board = (FrameLayout)findViewById(;
		 part1 = new View(this);
		 part1 = findViewById(;

	    	Paint paint = new Paint();
			Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),R.drawable.image);

	        int targetWidth  = 300;
	        int targetHeight = 300;

	        Bitmap targetBitmap = Bitmap.createBitmap(targetWidth, targetHeight,Bitmap.Config.ARGB_8888);

	        RectF rectf = new RectF(0, 0, 100, 100);

	        Canvas canvas = new Canvas(targetBitmap);
	        Path path = new Path();

	        path.addRect(rectf, Path.Direction.CW);

	        canvas.drawBitmap( bitmapOrg, new Rect(0, 0, bitmapOrg.getWidth(), bitmapOrg.getHeight()),
	       		 			new Rect(0, 0, targetWidth, targetHeight), paint);

	        Matrix matrix = new Matrix();
	        matrix.postScale(1f, 1f);
	        Bitmap resizedBitmap = Bitmap.createBitmap(targetBitmap, 0, 0, 100, 100, matrix, true);

	        /*convert Bitmap to resource */
	        BitmapDrawable bd = new BitmapDrawable(resizedBitmap);


    	catch(Exception e){
    		System.out.println("Error1 : " + e.getMessage() + e.toString());



<?xml version="1.0" encoding="utf-8"?>



crop image in android Example

cropping image in android

If you got any errors please let me know.



  1. ravi March 23, 2012 at 4:04 pm

    error occur….
    null java.lang.nullpointerexception…..
    pls help me

    • ravi March 23, 2012 at 4:11 pm

      OK… SOLVE IT.. THX

  2. ravi March 23, 2012 at 4:10 pm

    thax…….. error.. solve……….. by mistake. i am taking another xml file for view framelayout……. thax agmain

  3. Pingback: Checking a camera or image for pixel size : Android Community - For Application Development

  4. Personalised Prints May 2, 2013 at 11:42 am

    Really Nice!, Really happy i came accross this.

  5. Criminal Case Hack August 23, 2013 at 8:58 am

    Hi there, I enjoy reading through your article post. I like to
    write a little comment to support you.

  6. azahar November 28, 2013 at 1:16 pm

    I have error on following line
    <?xml version="1.0" encoding=”utf-8″?>

    • James December 7, 2013 at 1:52 pm

      Try to type the quotes once again.

  7. austin March 26, 2014 at 3:04 pm

    i want to start the cropping from 200 down with a height of 100. returned bitmap is blank.
    i only modify RectF() to RectF(0 , 200 , bitmapOrg.width , 100).
    can somebody please help me out

  8. Pingback: 从Android中的图库中选择时裁剪图像 Android Cookie

  9. Pingback: Taglia un&apos;image quando selezionata dalla galleria in android IL ANDORID

  10. Pingback: Crop an image when selected from gallery in android Android Babe

  11. Pingback: Cortair uma image quando selecionada da galeria no Android Android Cake

  12. Pingback: 在Android中从图库中select图像时裁剪图像 Android Cookie

Leave a Reply

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




Theme by HermesThemes

Copyright © 2018 CoderzHeaven. All Rights Reserved

Please wait...

Subscribe to our newsletter

Want to be notified when our article is published? Enter your email address and name below to be the first to know.