int size = bitmap.getRowBytes() * bitmap.getHeight();
获取大小,Bitmap所占用的内存空间数等于Bitmap的每一行所占用的空间数乘以Bitmap的行数
可以获取Bitmap大小的方法有如下几种:
1. getRowBytes:Since API Level 1
2. getByteCount:Since API Level 12
3. getAllocationByteCount:Since API Level 19
兼容性解决方案:
public static int getBitmapSize(Bitmap bitmap) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { //SInce API 19
return bitmap.getAllocationByteCount();
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR1) { //Since API 12
return bitmap.getByteCount();
}
return bitmap.getRowBytes() * bitmap.getHeight();
}
Note:
如果位图被重用、解码更小尺寸的其他位图或重新配置,getAllocationByteCount()的计算结果有可能比getByteCount()的结果大一些。
本文介绍如何使用不同API级别下的方法来获取Bitmap对象所占用的内存大小,并提供了一个兼容多种Android版本的解决方案。

862

被折叠的 条评论
为什么被折叠?



