Bitmap byte-size after decoding?

It's best to just use the support library:

int bitmapByteCount=BitmapCompat.getAllocationByteCount(bitmap)

But if you have the Android project to use at least minSdk of 19 (kitkat, meaning 4.4), you can just use bitmap.getAllocationByteCount() .


Here is the 2014 version that utilizes KitKat's getAllocationByteCount() and is written so that the compiler understands the version logic (so @TargetApi is not needed)

/**
 * returns the bytesize of the give bitmap
 */
public static int byteSizeOf(Bitmap bitmap) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        return bitmap.getAllocationByteCount();
    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR1) {
        return bitmap.getByteCount();
    } else {
        return bitmap.getRowBytes() * bitmap.getHeight();
    }
}

Note that the result of getAllocationByteCount() can be larger than the result of getByteCount() if a bitmap is reused to decode other bitmaps of smaller size, or by manual reconfiguration.


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();
        }
    }

Tags:

Android

Bitmap