有没有办法根据图像宽度、高度和文件大小确定 Bitmap.getByteCount?
基于
/** @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
/** @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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
ImageView 内存没有真正的限制,但加载/解码的位图有限制,这取决于设备/硬件。通常它是“4096 x 4096 像素 x 4 (argb)”= 接近 70MB,但某些设备可能有 8192x8192x4。
您可以使用这个(伪代码):
然后检查其 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):
then check its ColorSpace (ARGB or just RGB) and moltiply W x H x 3_or_4 to get Total bytes.