Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bitmap byte-size after decoding?

Tags:

android

bitmap

How can I determine/calculate the byte size of a bitmap (after decoding with BitmapFactory)? I need to know how much memory space it occupies, because I'm doing memory caching/management in my app. (file size is not enough, since these are jpg/png files)

Thanks for any solutions!

Update: getRowBytes * getHeight might do the trick.. I'll implement it this way until someone comes up with something against it.

like image 543
user289463 Avatar asked Mar 09 '10 08:03

user289463


People also ask

How do I convert a bitmap to a byte?

In addition, you can simply convert byte array to Bitmap . var bmp = new Bitmap(new MemoryStream(imgByte)); You can also get Bitmap from file Path directly.


1 Answers

getRowBytes() * getHeight() seems to be working fine to me.

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

----Sample code

    @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();         }     } 
like image 96
user289463 Avatar answered Sep 19 '22 07:09

user289463