How to Resize a Bitmap in Android? How to Resize a Bitmap in Android? android android

How to Resize a Bitmap in Android?


Change:

profileImage.setImageBitmap(    BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length)

To:

Bitmap b = BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length)profileImage.setImageBitmap(Bitmap.createScaledBitmap(b, 120, 120, false));


import android.graphics.Matrixpublic Bitmap getResizedBitmap(Bitmap bm, int newWidth, int newHeight) {    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);    bm.recycle();    return resizedBitmap;}

EDIT: as suggested by by @aveschini, I have added bm.recycle(); for memory leaks. Please note that in case if you are using the previous object for some other purposes, then handle accordingly.


If you already have a bitmap, you could use the following code to resize:

Bitmap originalBitmap = <original initialization>;Bitmap resizedBitmap = Bitmap.createScaledBitmap(    originalBitmap, newWidth, newHeight, false);