Skip to content

Resizing a Bitmap

December 25, 2009

Hey everyone,

Below is a snippet of code that will allow you to resize a Bitmap.


public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {

int width = bm.getWidth();

int height = bm.getHeight();

float scaleWidth = ((float) newWidth) / width;

float scaleHeight = ((float) newHeight) / height;

// create a matrix for the manipulation

Matrix matrix = new Matrix();

// resize the bit map

matrix.postScale(scaleWidth, scaleHeight);

// recreate the new Bitmap

Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);

return resizedBitmap;

}

Pretty self explanatory – simply input the original Bitmap object and the desired dimensions of the Bitmap, and this method will return to you the newly resized Bitmap!

– jwei

18 Comments leave one →
  1. david permalink
    January 8, 2010 12:41 pm

    android.graphics.Bitmap.createScaledBitmap should do the same.

    • Dmitry permalink
      October 10, 2010 8:21 pm

      and with “filter = true” – much better. Thanks a lot.

  2. July 29, 2010 7:56 pm

    Thanks david, that’s much better!

  3. September 12, 2011 9:13 am

    Excellent work… thanks for saving my time

  4. September 20, 2011 10:28 pm

    thank you. This blog has help me a lot

  5. Naresh permalink
    September 26, 2011 5:07 am

    Thank you….!

  6. October 21, 2011 5:16 am

    hi,

    Thanks for the funciton. It perfectly worked when I used the same but it is making my photo blur. I want to take high Quality picture from Camera and save it in file of format .png.

    But I am not able to save it to high resolution. It is only taking picture of my android phone size. 😦 Any tips?

    • cking24343 permalink
      November 12, 2011 9:47 pm

      same issue, have you been able to resolve this yet?

  7. November 7, 2011 7:10 am

    great job dude!!

  8. Mr.Bells permalink
    December 7, 2011 9:04 pm

    To resolve the Photo blur, always give the original bitmap as an input. Change only the width and height of the Bitmap to resize

  9. Swati permalink
    December 24, 2011 5:41 am

    Hi. I tried android.graphics.Bitmap.createScaledBitmap method, but when i use it, my bitmap is scaled but the image’s ratios change.

  10. hanoun permalink
    April 22, 2012 1:06 pm

    hello, when i convert byte to biitmap it, returns null , i have a bitmap and compare to other in database so i have to convert byte to bitmap in order to compare it, can someone help me please
    if(b.length!=0){
    return BitmapFactory.decodeByteArray(b, 0, b.length);
    }
    else {
    return null;
    }

  11. Prateek permalink
    July 10, 2012 2:34 am

    Thank you

  12. loloof64 permalink
    August 10, 2012 8:34 am

    Thank you very much for this code πŸ™‚

  13. Akhter permalink
    January 1, 2013 9:43 pm

    good job.. but it giving blur effect . and i am using the orignal image which is of size 1280×800 and i want to convert it to 1024×600. but on result image is giving too much blur

Trackbacks

  1. Android Resize Bitmap « tediscript.wordpress.com
  2. how to save image into same dimension from different orientation. - Android Forums
  3. image question - Android Forums

Leave a comment