当我在将图像保存到 SD 卡之前搜索如何查找图像的大小时,我发现了这个:
bitmap.getByteCount();
但该方法是在 API 12 中添加的,我正在使用 API 10。所以我再次发现了这一点:
getByteCount() 只是一种方便的方法,它完全按照您在 else block 中的操作。换句话说,如果你简单地重写 getSizeInBytes 总是返回 "bitmap.getRowBytes() * bitmap.getHeight()"
这里:
Where the heck is Bitmap getByteCount()?
所以,通过计算这个 bitmap.getRowBytes() * bitmap.getHeight()
我得到了值 120000 (117 KB)
。
其中 SD 卡上的图像大小为 1.6 KB
。
我错过了什么?还是做错了?
谢谢
最佳答案
你做得对!
确定这些值是否有效的快速方法是这样记录:
int numBytesByRow = bitmap.getRowBytes() * bitmap.getHeight();
int numBytesByCount = bitmap.getByteCount();
Log.v( TAG, "numBytesByRow=" + numBytesByRow );
Log.v( TAG, "numBytesByCount=" + numBytesByCount );
这给出了结果:
03-29 17:31:10.493: V/ImageCache(19704): numBytesByRow=270000
03-29 17:31:10.493: V/ImageCache(19704): numBytesByCount=270000
所以两者都在计算相同的数字,我怀疑这是位图的内存大小。这与 JPG 或 PNG 在磁盘上,因为它是完全未压缩的。
有关更多信息,我们可以查看 AOSP 和示例项目中的源代码。这是在 Android 开发者文档 Caching Bitmaps 中的示例项目 BitmapFun 中使用的文件
/**
* Get the size in bytes of a bitmap in a BitmapDrawable.
* @param value
* @return size in bytes
*/
@TargetApi(12)
public static int getBitmapSize(BitmapDrawable value) {
Bitmap bitmap = value.getBitmap();
if (APIUtil.hasHoneycombMR1()) {
return bitmap.getByteCount();
}
// Pre HC-MR1
return bitmap.getRowBytes() * bitmap.getHeight();
}
正如你所看到的,这与他们使用的技术相同
bitmap.getRowBytes() * bitmap.getHeight();
引用资料:
关于Android:API 中的 bitmap.getByteCount() 小于 12,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12581322/