有没有办法根据图像宽度、高度和文件大小确定 Bitmap.getByteCount?

发布于 2025-01-14 07:14:42 字数 1347 浏览 2 评论 0原文

基于

https:// android.googlesource.com/platform/frameworks/base/+/master/graphics/java/android/graphics/RecordingCanvas.java

/** @hide */
private static int getPanelFrameSize() {
    final int DefaultSize = 100 * 1024 * 1024; // 100 MB;
    return Math.max(SystemProperties.getInt("ro.hwui.max_texture_allocation_size", DefaultSize),
            DefaultSize);
}
/** @hide */
public static final int MAX_BITMAP_SIZE = getPanelFrameSize();

/** @hide */
@Override
protected void throwIfCannotDraw(Bitmap bitmap) {
    super.throwIfCannotDraw(bitmap);
    int bitmapSize = bitmap.getByteCount();
    if (bitmapSize > MAX_BITMAP_SIZE) {
        throw new RuntimeException(
                "Canvas: trying to draw too large(" + bitmapSize + "bytes) bitmap.");
    }
}

有一个硬限制 100MB bitmap.getByteCount() 强加于 ImageView

有什么方法可以计算出bitmap.getByteCount(),而不需要对图像文件本身执行 I/O 读取。

这将帮助我们确定如何将图像加载到 ImageView 中,而不会达到硬限制。

目前,在我们的数据库中,我们存储图像文件的以下元数据。

- Image file path
- Image type (PNG or JPG)
- Image file size in byte
- Image width
- Image height

谢谢。

Based on

https://android.googlesource.com/platform/frameworks/base/+/master/graphics/java/android/graphics/RecordingCanvas.java

/** @hide */
private static int getPanelFrameSize() {
    final int DefaultSize = 100 * 1024 * 1024; // 100 MB;
    return Math.max(SystemProperties.getInt("ro.hwui.max_texture_allocation_size", DefaultSize),
            DefaultSize);
}
/** @hide */
public static final int MAX_BITMAP_SIZE = getPanelFrameSize();

/** @hide */
@Override
protected void throwIfCannotDraw(Bitmap bitmap) {
    super.throwIfCannotDraw(bitmap);
    int bitmapSize = bitmap.getByteCount();
    if (bitmapSize > MAX_BITMAP_SIZE) {
        throw new RuntimeException(
                "Canvas: trying to draw too large(" + bitmapSize + "bytes) bitmap.");
    }
}

There is a hard limit 100MB bitmap.getByteCount() imposed on ImageView.

Is there any way we can figure out bitmap.getByteCount(), without perform I/O reading on image file itself.

This will help us to determine how we want to load the image into ImageView, without hitting the hard limit.

Currently, in our DB, we store the following meta data of the image file.

- Image file path
- Image type (PNG or JPG)
- Image file size in byte
- Image width
- Image height

Thanks.

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

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

发布评论

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

评论(1

夜雨飘雪 2025-01-21 07:14:42

ImageView 内存没有真正的限制,但加载/解码的位图有限制,这取决于设备/硬件。通常它是“4096 x 4096 像素 x 4 (argb)”= 接近 70MB,但某些设备可能有 8192x8192x4。

您可以使用这个(伪代码):

options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filepath, options)
final int H = options.outHeight;
final int W = options.outWidth;

然后检查其 ColorSpace(ARGB 或只是 RGB)并 moltiply W x H x 3_or_4 以获得总字节数。

There isn't a really limit for ImageView Memory, but there is a limit for loaded/decoded Bitmap and it depends on Device/hardware. Usually it is "4096 x 4096 pixels x 4 (argb)" = near 70MB, but some device could have 8192x8192x4.

You can use this (pseudo code):

options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filepath, options)
final int H = options.outHeight;
final int W = options.outWidth;

then check its ColorSpace (ARGB or just RGB) and moltiply W x H x 3_or_4 to get Total bytes.

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