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
Just check the file size after you've created it using file. length : developer.android.com/reference/java/io/File.html#length() .
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.
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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With