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).