replace image in Bitmap object without allocating memory

All your problems with Audio, Video and Images.

replace image in Bitmap object without allocating memory

Postby SystemOfADan » Fri Mar 12, 2010 8:46 pm

Hello,

I would like to know how to swap a PNG image file displayed in a Bitmap object without allocating or releasing any memory.

I am working on a application that uses a cache of Bitmap image tiles. When a new PNG image needs to be loaded from the sdcard, the application currently uses BitmapFactory.decodeByteArray to create a new Bitmap object, removes the old Bitmap object from the cache and adds the new Bitmap to it. This means that a Bitmap is allocated and another is released each time a tile needs to be swapped from the sdcard. This makes the GC work very hard and eventually leads to an "OutOfMemoryError: bitmap size exceeds VM budget" error.

I would like to know how to directly overwrite the data in an existing Bitmap object with data from a PNG file. The copyPixelsFromBuffer(Buffer src) and setPixels(int[] pixels, int offset, int stride, int x, int y, int width, int height) methods in Bitmap look promising, if I new how to decode a PNG file into a Buffer or int array (also without allocating or releasing any memory each time it runs).

Thanks,
Dan
SystemOfADan
Freshman
Freshman
 
Posts: 2
Joined: Fri Mar 12, 2010 7:55 pm

Top

Postby mrco » Tue Mar 16, 2010 8:39 am

You should use bitmap.recycle method.
mrco
Experienced Developer
Experienced Developer
 
Posts: 71
Joined: Fri Jan 15, 2010 11:31 am
Location: 2V Software

replace image in Bitmap object without allocating

Postby SystemOfADan » Thu Mar 18, 2010 5:57 pm

Thanks, we are now using bitmap.recycle and it does appear to fix the OutOfMemoryError.

I still think it would be even better to resuse existing memory, rather than allocating and releasing Bitmap objects each time a new image is loaded.

Thanks,
Dan
SystemOfADan
Freshman
Freshman
 
Posts: 2
Joined: Fri Mar 12, 2010 7:55 pm

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: No registered users and 10 guests