Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where the heck is Bitmap getByteCount()?

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()
like image 847
Astronaut Avatar asked Jun 21 '11 15:06

Astronaut


2 Answers

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();
    }
}
like image 144
Jeremy Edwards Avatar answered Sep 28 '22 04:09

Jeremy Edwards


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.

like image 41
Matt Ball Avatar answered Sep 28 '22 02:09

Matt Ball