3

There is a useful method bitmap.getByteCount() since API level 12. But how to get same value in API 11?

Dmitry Zaytsev
  • 23,650
  • 14
  • 92
  • 146

2 Answers2

19

As mentioned by dmon, according to the comments of this question bitmap.getByteCount() is just a convenience method which returns bitmap.getRowBytes() * bitmap.getHeight(). So you can use a custom method instead :

public static long getSizeInBytes(Bitmap bitmap) {
    return bitmap.getRowBytes() * bitmap.getHeight();
}
Community
  • 1
  • 1
Dalmas
  • 26,409
  • 9
  • 67
  • 80
  • 1
    Like the commenters said in that answer, you don't need the if-else block. Just use the else bit. That way you don't need AndroidVersion. – dmon Jun 21 '12 at 13:52
7

It's best to just use the support library:

int bitmapByteCount=BitmapCompat.getAllocationByteCount(bitmap)

or, if you wish to do it yourself:

public static int getBitmapByteCount(Bitmap bitmap) {
    if (VERSION.SDK_INT < VERSION_CODES.HONEYCOMB_MR1)
        return bitmap.getRowBytes() * bitmap.getHeight();
    if (VERSION.SDK_INT < VERSION_CODES.KITKAT)
        return bitmap.getByteCount();
    return bitmap.getAllocationByteCount();
}
android developer
  • 114,585
  • 152
  • 739
  • 1,270