I know the Android platform is a huge mess, over complicated and over-engineered, but seriously to get the size of a bitmap, is it really necessary to do all those conversions?
Bitmap bitmap = your bitmap object
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] imageInByte = stream.toByteArray();
long length = imageInByte.length;
According to Google Documentation Bitmap has a method getByteCount() to do this, however it is not present in SDK2.2, haven't tried other's but there is no mention of it being deprecated or that API support is any different from API 1... So where is this mysterious method hiding? It would really nice to be albe to simple do
bitmap.getByteCount()
I just wrote this method. AndroidVersion.java is a class I created to easily get me the version code from the phone.
http://code.google.com/p/android-beryl/source/browse/beryl/src/org/beryl/app/AndroidVersion.java
public static long getSizeInBytes(Bitmap bitmap) {
if(AndroidVersion.isHoneycombMr2OrHigher()) {
return bitmap.getByteCount();
} else {
return bitmap.getRowBytes() * bitmap.getHeight();
}
}
If you filter by API Level 8 (= SDK 2.2), you'll see that Bitmap#getByteCount()
is greyed out, meaning that method is not present in that API level.
getByteCount()
was added in API Level 12.
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