Thursday, January 31, 2013

Saving bitmap buffer as bitmap image

Eclipse Memory Analyzer doesn’t provide option to view a bitmap buffer directly. Try the following steps that will allow you to save the bitmap buffer as a bitmap image that you can view using any image viewer.

1. Select the bitmap buffer that you would like to Save


domtree



2. Save the buffer to your computer


Select the Location in your computer where you would like to save the buffer file. You can give any name and extension
savebuffer



3. Enable inspector view in Eclipse


Once you have saved the bitmap buffer you will have to convert the bitmap buffer to bitmap for which you will need to know height and width of the bitmap.

You can use the inspector window to view the height and width of the bitmap. If inspector window is not available you can enable it using the following option.

inspectorview


showview



4. Find the dimension of the bitmap


imagedim



5. Convert the bitmap buffer in to bitmap image


Once you have the bitmap buffer, bitmap dimension you can execute the attached java program with the following command line option.

[code]
Java MATBitmap.java <input.bmp> <output.bmp> <bitmap width> <bitmap height>
[/code]

Example

[code]
java MATBitmap debug.bmp converted.bmp 800 800
[/code]

Bingo! now you can open the converted.bmp in any image editor.

Download MATBitmap.java

Fixing Bitmap memory leaks in Android

The biggest hurdle for Android developers is memory leaks. Especially if you are using a lot of images you tend to see memory leaks more often. Android internally handles the images as Bitmaps so even if the images used are optimized or of low quality it takes the same amount of memory. For example an image of size 800X600 takes 4 bytes per pixel which means the size will be 1.8 MB (4*800*600 = 1920000 bytes) whereas the same image will be 200 KB – 600 KB based on the quality of the image.
If you are facing frequent Out Of Memory errors then the first handy tool will be DDMS and Memory Analyzer tool (MAT).

Whenever you take memory dump you could see that there are bitmaps held in heap but most of the time you will not be able to find which image exactly the bitmap instance holds. MAT doesn’t provide an easy way to view the bitmap image held in a bitmap objects.
You can do the following steps to view the image in the bitmap buffer.