Bitmap byte-size after decoding?
Asked Answered
L

4

76

How can I determine/calculate the byte size of a bitmap (after decoding with BitmapFactory)? I need to know how much memory space it occupies, because I'm doing memory caching/management in my app. (file size is not enough, since these are jpg/png files)

Thanks for any solutions!

Update: getRowBytes * getHeight might do the trick.. I'll implement it this way until someone comes up with something against it.

Ld answered 9/3, 2010 at 8:30 Comment(0)
L
125

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();
        }
    }
Ld answered 9/3, 2010 at 10:12 Comment(6)
Tried it like this: original = BitmapFactory.decodeStream(getAssets().open("hd.jpg")); sizeOf(original); ByteArrayOutputStream out = new ByteArrayOutputStream(); original.compress(Bitmap.CompressFormat.WEBP, 50, out); Bitmap decoded = BitmapFactory.decodeStream(new ByteArrayInputStream(out.toByteArray())); sizeOf(decoded); When I debug inside your method, I get the same number of bytes even when the second image is compressed! Any comments?Hydrogenolysis
@Hydrogenolysis A Bitmap always contains the uncompressed image. Compression is only used to shrink an image for saving to a file.Jim
time to update and add the kitkat method of getAllocationByteCount(). See developer.android.com/reference/android/graphics/…Locarno
i have one image but if i run it into two different image then in both device i m getting different image size any reason? for example in Galaxy S Duos it gives 656 KB and in MOTO G device it return 1136 KBLovesick
@Kalpesh Your samsung has a hdpi screen and the moto a xhdpi, so it loads resources in different sizes on these models. Check out the google display densitiy bucket doc.Stylize
I am getting allocation byte count 3145728 using BitmapCompat.getAllocationByteCount(scaledBitmap). But the real size is 207kB. What am i doing wrong?Hagiocracy
T
63

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() .

Tufted answered 29/7, 2015 at 7:45 Comment(8)
I am getting allocation byte count 3145728 using BitmapCompat.getAllocationByteCount(scaledBitmap). But the real size is 207kB. What am i doing wrong?Hagiocracy
@Sniper Real size of what? If you mean the file size, it makes sense, because the file is compressed. Nobody these days will put the image into a file without encoding it into JPG/PNG/WEBP. The files aren't supposed to take the same size as the decoded image. The estimated byte count for each image is width*height*bytesPerPixel , where bytesPerPixel is usually 4 or 2. This means that if you have a 1000x1000 image, it could take about 4*1000*1000= 4,000,000 bytes, meaning ~4MB.Tufted
I have a bitmap in my onactivityResult and I want to check the size of the image and check whether the size is greater than 5 mb or not and then send it to my server if less than 5 mb otherwise prompt user to take a new picture.Hagiocracy
@Sniper You send an image file to the server. This is usually JPEG,PNG, WEBP... All of those usually take less than 5MB in storage space because they are compressed. They also almost always take less storage space than the bitmap takes in memory. Just check the file size after you've created it using file.length : developer.android.com/reference/java/io/File.html#length() . It has nothing to do with the bitmap. The bitmap resolution might be huge or small. What you talk about is the file itself.Tufted
Thanks @androiddeveloper for clarification.Hagiocracy
@Sniper If you have any more questions about it, you are free to ask :)Tufted
I do have about other Android issues. Can you help me?Hagiocracy
Sure, ask it as a new question here, instead of a comment, and I will see what I can do. If you have a bug report about Android or a suggestion, you can write here: issuetracker.google.com/issuesTufted
S
22

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.

Stylize answered 30/5, 2014 at 19:49 Comment(2)
Yes, Picasso also use this function to calculate bitmap size.Skillless
I am getting allocation byte count 3145728 using BitmapCompat.getAllocationByteCount(scaledBitmap). But the real size is 207kB. What am i doing wrong?Hagiocracy
C
8
public static int sizeOf(Bitmap data) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB_MR1) {
        return data.getRowBytes() * data.getHeight();
    } else if (Build.VERSION.SDK_INT<Build.VERSION_CODES.KITKAT){
        return data.getByteCount();
    } else{
        return data.getAllocationByteCount();
    }
}

The only difference with @user289463 answer, is the use of getAllocationByteCount() for KitKat and above versions.

Crites answered 10/1, 2015 at 4:5 Comment(2)
I am getting allocation byte count 3145728 using BitmapCompat.getAllocationByteCount(scaledBitmap). But the real size is 207kB. What am i doing wrong?Hagiocracy
@Sniper That 207kB value is the compressed file size when it is stored on disk. When it is loaded into memory, it is decompressed and uses the byte count you are getting from the function.Nopar

© 2022 - 2024 — McMap. All rights reserved.