Get Bitmap Size Get Free Memory Exception Android

Bitmap size calculation:
bmpHeight * bmpWidth
For example:
Resolution of image 1024×860 = 880 640 pixels
If every pixel get 4 byte of memory:
880 640×4= 3 522 560 (3.5MB)

Get bitmap size without allocation of memory:

   	BitmapFactory.Options options = new BitmapFactory.Options();
        // If set to true, the decoder will return null (no bitmap), but the out... fields will still
        // be set, allowing the caller to query the bitmap without having to allocate the memory for its pixels. 
    	options.inJustDecodeBounds = true; 
    	BitmapFactory.decodeResource(getResources(), R.drawable.my_image, options);
    	int imageHeight = options.outHeight; // 1024
    	int imageWidth = options.outWidth; // 860
    	String imageType = options.outMimeType; // .jpg .png .gif

Get Memory size:
Make your bitmap not bigger as maxMemory size
final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);

    float density = getResources().getDisplayMetrics().density;
 
    Debug.MemoryInfo memoryInfo = new Debug.MemoryInfo();
    Debug.getMemoryInfo(memoryInfo);
    final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
    final int freeMemory = (int) (Runtime.getRuntime().freeMemory() / 1024);
    String memMessage = String.format(
        "Free=%d kB,\nMaxMem=%d kB,\n Memory: Pss=%.2f MB, Private=%.2f MB, Shared=%.2f MB",
        freeMemory,
        maxMemory,
        memoryInfo.getTotalPss() / 1024.0,
        memoryInfo.getTotalPrivateDirty() / 1024.0,
        memoryInfo.getTotalSharedDirty() / 1024.0);
    ((TextView)findViewById(R.id.textViewInfo)).setText(memMessage );
This entry was posted in Android Examples Code, Bitmap, drawing and tagged , . Bookmark the permalink.


Comments are closed.



10,400 views