解码后的位图字节大小?位图、字节、大小

2023-09-12 11:20:19 作者:孤寂

我如何能够确定/计算(与BitmapFactory解码之后)的位图的字节大小? 我需要知道有多少内存空间占用,因为我在做内存缓存/管理我的应用程序。 (文件大小是不够的,因为这些是JPG / PNG文件)

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)

感谢您的解决方案!

更新:getRowBytes *的getHeight可能做的伎俩。我将实现这种方式,直到有人想出了一些反对

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

推荐答案

getRowBytes()*的getHeight()似乎是工作的罚款我。

getRowBytes() * getHeight() seems to be working fine to me.

更新了我的〜2岁的答案: 由于API级别12位图有一个直接的方式来查询的字节大小: http://developer.android.com/reference/android/graphics/Bitmap.html#getByteCount%28%29

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

----样品code

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