解码后位图字节大小?

发布于 2024-08-24 14:04:17 字数 194 浏览 6 评论 0 原文

如何确定/计算位图的字节大小(使用 BitmapFactory 解码后)? 我需要知道它占用了多少内存空间,因为我正在我的应用程序中进行内存缓存/管理。 (文件大小不够,因为这些是 jpg/png 文件)

感谢您提供任何解决方案!

更新: getRowBytes * getHeight 可能会成功。我会这样实现它,直到有人提出反对它的东西。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

宫墨修音 2024-08-31 14:04:17

getRowBytes() * getHeight() 对我来说似乎工作得很好。

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

----示例代码

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

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();
        }
    }
酒解孤独 2024-08-31 14:04:17

最好只使用支持库:

int bitmapByteCount=BitmapCompat.getAllocationByteCount(bitmap)

但是如果您的 Android 项目至少使用 19 的 minSdk(kitkat,意思是 4.4),则可以只使用 bitmap.getAllocationByteCount()

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

忆沫 2024-08-31 14:04:17

这是利用 KitKat 的 getAllocationByteCount() 的编写是为了让编译器理解版本逻辑(因此不需要 @TargetApi

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

请注意 getAllocationByteCount() 可以大于getByteCount() 如果重用位图来解码其他较小尺寸的位图,或者通过手动重新配置。

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.

你的往事 2024-08-31 14:04:17
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();
    }
}

与 @user289463 答案的唯一区别是 KitKat 及以上版本使用 getAllocationByteCount()

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文