0

I am trying to scale a Bitmap, doubling its size.

But after the scaling the bitmap shows empty, all plain gray...

here is the code:

Matrix matrix = new Matrix();

    // resize the bit map
    matrix.postScale(2, 2);

    // recreate the new Bitmap and set it back
    Bitmap bm2=Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, true);   
    //bm.recycle();

EDIT EDIT EDIT EDIT EDIT

I figured it out it's memory issue, if I do it with small images it works fine.

Still the problem remains with large pictures!!!

Thanks for any suggestion!!!

Lisa Anne
  • 4,482
  • 17
  • 83
  • 157

1 Answers1

0

As from the Bitmap Doc it is clearly saying that Bitmap.createBitmap() returns the same bitmap or part of the source bitmap.So may be here it returns the same bitmap object.But here you are recycling it

bm.recycle();

thats why you are getting null

Use this method

    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;
    Matrix matrix = new Matrix();
    matrix.postScale(scaleWidth, scaleHeight);

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

and pass width as 1920 and height as 2560

kalyan pvs
  • 14,486
  • 4
  • 41
  • 59