Bit size after decoding?

How to determine / calculate the byte size of a bitmap (after decoding with BitmapFactory)? I need to know how much memory it takes, because I do caching / memory management in my application. (file size is insufficient, as these are jpg / png files)

Thanks for any solutions!

Update: getRowBytes * getHeight can do the trick. I implement it that way until someone comes up with something against him.

+53
android bitmap
Mar 09 '10 at 8:30
source share
4 answers

getRowBytes() * getHeight() seems to work fine for me.

Update my answer for 2 years: Since the API level 12 bitmap has a direct way to request byte size: http://developer.android.com/reference/android/graphics/Bitmap.html#getByteCount%28%29

---- Code Example

  @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1) protected int sizeOf(Bitmap data) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB_MR1) { return data.getRowBytes() * data.getHeight(); } else { return data.getByteCount(); } } 
+108
Mar 09 '10 at 10:12
source share

It is best to use a support library:

 int bitmapByteCount=BitmapCompat.getAllocationByteCount(bitmap) 
+27
Jul 29 '15 at 7:45
source share

Here is the version of 2014 that uses KitKat getAllocationByteCount() and is written so that the compiler understands the logic of the version (so @TargetApi not required)

 /** * returns the bytesize of the give bitmap */ public static int byteSizeOf(Bitmap bitmap) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { return bitmap.getAllocationByteCount(); } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR1) { return bitmap.getByteCount(); } else { return bitmap.getRowBytes() * bitmap.getHeight(); } } 

Note that the result of getAllocationByteCount() may be larger than the result of getByteCount() if the bitmap is reused to decode other smaller bitmaps or by manual reconfiguration.

+20
May 30 '14 at 19:49
source share
 public static int sizeOf(Bitmap data) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB_MR1) { return data.getRowBytes() * data.getHeight(); } else if (Build.VERSION.SDK_INT<Build.VERSION_CODES.KITKAT){ return data.getByteCount(); } else{ return data.getAllocationByteCount(); } } 

The only difference from @ user289463's answer is using getAllocationByteCount() for KitKat and higher versions.

+5
Jan 10
source share



All Articles