10

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()
Astronaut
  • 6,691
  • 18
  • 61
  • 99

7 Answers7

61

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();
    }
}
Jeremy Edwards
  • 14,620
  • 17
  • 74
  • 99
  • 18
    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()" you don't need the Android version check. – Johan Pelgrim Apr 05 '12 at 13:19
  • 1
    you should return `(bitmap.getRowBytes() * bitmap.getHeight()) / 1024;` – Ram Patra Feb 09 '14 at 14:41
12

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.

Matt Ball
  • 354,903
  • 100
  • 647
  • 710
3

The answers here are a bit outdated. Reason (in the docs) :

getByteCount : As of KITKAT, the result of this method can no longer be used to determine memory usage of a bitmap. See getAllocationByteCount().

So, the current answer should be :

int result=BitmapCompat.getAllocationByteCount(bitmap)

or, if you insist on writing 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
1

Before API 12 you can calculate the byte size of an Bitmap using getHeight() * getWidth() * 4 if you are using ARGB_8888 because every pixel is stored in 4bytes. I think this is the default format.

fr4gus
  • 396
  • 7
  • 18
1

As mentioned in other answers, it is only available on API 12 or higher. This is a simple compatibility version of the method.

public static int getByteCount(Bitmap bitmap) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB_MR1) {
        return bitmap.getRowBytes() * bitmap.getHeight();
    } else {
        return bitmap.getByteCount();
    }
}
f2prateek
  • 2,034
  • 2
  • 20
  • 26
0

I tried all of the above methods and they were close, but not quite right (for my situation at least).

I was using bitmap.getByteCount(); inside of the sizeOf() method when creating a new LruCache:

mMemoryCache = new LruCache<String, Bitmap>(cacheSize) {
    @Override
    protected int sizeOf(String key, Bitmap bitmap) {
        return bitmap.getByteCount();
    }
};  

I then tried the suggested:

return bitmap.getRowBytes() * bitmap.getHeight();

This was great, but I noticed that the returned values were different and when I used the suggestion above, it would not even make a cache on my device. I tested the return values on a Nexus One running api 3.2 and a Galaxy Nexus running 4.2:

bitmap.getByteCount(); returned-> 15  
bitmap.getRowBytes() * bitmap.getHeight(); returned-> 15400

So to solve my issue, I simply did this:

return (bitmap.getRowBytes() * bitmap.getHeight()) / 1000; 

instead of:

return bitmap.getByteCount();  

May not be the same situation you were in, but this worked for me.

levibostian
  • 753
  • 1
  • 7
  • 21
  • everywhere I look, every source I read, all I see it `return (bitmap.getRowBytes() * bitmap.getHeight()) / 1000;`. It must be the type of file I am saving or something. So next time I need to use `bitmap.getByteCount();`, I will do some testing to see what value I need first. – levibostian Jun 06 '13 at 21:43
  • you should return `(bitmap.getRowBytes() * bitmap.getHeight()) / 1024;` instead. – Ram Patra Feb 09 '14 at 14:41
0

As you can see in the source code, getByteCount is simply this:

public final int getByteCount() {
    // int result permits bitmaps up to 46,340 x 46,340
    return getRowBytes() * getHeight();
}

Here is the source code for 5.0

javmarina
  • 493
  • 6
  • 17