Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: bitmap.getByteCount() in API lesser than 12

Tags:

When i searched for how to find the size of an image before saving it on the SD card, i found this:

bitmap.getByteCount(); 

but that method is added in API 12 and i am using API 10. So again i found out this:

getByteCount() is just a convenience method which does exactly what you have placed in the else-block. In other words, if you simply rewrite getSizeInBytes to always return "bitmap.getRowBytes() * bitmap.getHeight()"

here:

Where the heck is Bitmap getByteCount()?

so, by calculating this bitmap.getRowBytes() * bitmap.getHeight() i got the value 120000 (117 KB).

where as the image size on the SD card is 1.6 KB.

What am i missing? or doing wrong?

Thank You

like image 855
Archie.bpgc Avatar asked Sep 25 '12 10:09

Archie.bpgc


People also ask

How do I know the size of my bitmap?

Just check the file size after you've created it using file. length : developer.android.com/reference/java/io/File.html#length() .

How do I know if my Android BMP is empty?

You can do a check when you want to return the BitMap look to see if the ArrayList of Paths is bigger than 0 and return the BitMap if so, or else return null.


2 Answers

You are doing it correctly!

A quick way to know for sure if the values are valid, is to log it like this:

int numBytesByRow = bitmap.getRowBytes() * bitmap.getHeight(); int numBytesByCount = bitmap.getByteCount(); Log.v( TAG, "numBytesByRow=" + numBytesByRow );  Log.v( TAG, "numBytesByCount=" + numBytesByCount );  

This gives the result:

03-29 17:31:10.493: V/ImageCache(19704): numBytesByRow=270000 03-29 17:31:10.493: V/ImageCache(19704): numBytesByCount=270000 

So both are calculating the same number, which I suspect is the in-memory size of the bitmap. This is different than a JPG or PNG on disk as it is completely uncompressed.


For more info, we can look to AOSP and the source in the example project. This is the file used in the example project BitmapFun in the Android developer docs Caching Bitmaps

AOSP ImageCache.java

/**  * Get the size in bytes of a bitmap in a BitmapDrawable.  * @param value  * @return size in bytes  */ @TargetApi(12) public static int getBitmapSize(BitmapDrawable value) {     Bitmap bitmap = value.getBitmap();      if (APIUtil.hasHoneycombMR1()) {         return bitmap.getByteCount();     }     // Pre HC-MR1     return bitmap.getRowBytes() * bitmap.getHeight(); } 

As you can see this is the same technique they use

bitmap.getRowBytes() * bitmap.getHeight(); 

References:

  • http://developer.android.com/training/displaying-bitmaps/cache-bitmap.html
  • http://code.google.com/p/adamkoch/source/browse/bitmapfun/
like image 56
pjco Avatar answered Nov 10 '22 00:11

pjco


For now i am using this:

ByteArrayOutputStream bao = new ByteArrayOutputStream(); my_bitmap.compress(Bitmap.CompressFormat.PNG, 100, bao); byte[] ba = bao.toByteArray(); int size = ba.length; 

to get total no.of bytes as size. Because the value i get here perfectly matches the size(in bytes) on the image on SD card.

like image 34
Archie.bpgc Avatar answered Nov 09 '22 23:11

Archie.bpgc