2

I wanted to scale image in order that they kept the same ratio. Thus for example, an arrow has the same size in all images after the rescaling. So I followed this example and it works fine. But after lost of manipulations of the listview, I can have an OutOfMemoryError error. I ckeck the heap dump in DDMS and that's right, the allocation size always goes up. I put some bitmap.recycle() but it leads to an error: "cannot draw recycled bitmaps".

I also tried the official tutorial, but I had problems, the downloaded sample is not the same as those explained, and I don't understand everything.

Please, can you explain me how to solve my mistake? Thanks

Community
  • 1
  • 1
Turvy
  • 882
  • 1
  • 8
  • 23
  • what do you mean by "some bitmap.recycle()"?? There *SHOULD*, and I mean ¡¡MUST!! be a recycle for each of the disposed bitmap instances. Unless, memory consumption will rise and rise. And, there´s no reason for it not to keep rising even when you dispose all of them if the user keeps provoking the reescale too fast. – eduyayo Sep 22 '14 at 12:31

1 Answers1

0

Bitmaps always stored in the device Heap Memory , and your device heap memory can't afford that number of Bitmaps as may be the way you try works on the same resolution of the image, So, at first try to grow the heap using your app manifest, then try this way to resize your Bitmaps:

public static Bitmap scaleBitmap(Bitmap bitmapToScale, float newWidth, float newHeight) {

        if(bitmapToScale == null)
            return null;
        //get the original width and height
        int width = bitmapToScale.getWidth();
        int height = bitmapToScale.getHeight();
        // create a matrix for the manipulation
        Matrix matrix = new Matrix();

        // resize the bit map
        matrix.postScale(newWidth / width, newHeight / height);

        // recreate the new Bitmap and set it back
        return Bitmap.createBitmap(bitmapToScale, 0, 0, bitmapToScale.getWidth(), bitmapToScale.getHeight(), matrix, true);  

    }

if this don't work either, so, you have to make one of the following:

  1. assign to each image a much less width and height.
  2. compress the image using something like ImageLoader.
  3. use another device with much Heap Space.
  4. limit the number of images used.

HINT: recycling the Bitmap totally removes it from the Heap memory, So, it's logical to have a "cannot draw recycled bitmaps" error, So, if you want to recycle it and then use it in your app, you have to store it at first in something like ArrayList<Bitmaps>.

Muhammed Refaat
  • 8,914
  • 14
  • 83
  • 118
  • I had already grown the heap in the manifest, and your solution is difficult since I don't know the final size of the image. Thanks for your answer – Turvy Sep 22 '14 at 15:16