如何确定/计算位图的字节大小(使用BitmapFactory解码后)?我需要知道它占用了多少内存空间,因为我正在我的应用程序中进行内存缓存/管理.(文件大小不够,因为这些是jpg/png文件)
谢谢你的解决方案!
更新:getRowBytes*getHeight可能会成功.我将以这种方式实现它,直到有人提出反对它的东西.
use*_*463 114
getRowBytes() * getHeight() 似乎对我很好.
更新到我~2岁的答案:由于API级别12 Bitmap有直接查询字节大小的方法: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();
}
}
Run Code Online (Sandbox Code Playgroud)
and*_*per 43
最好只使用支持库:
int bitmapByteCount=BitmapCompat.getAllocationByteCount(bitmap)
Run Code Online (Sandbox Code Playgroud)
pat*_*ckf 21
这是使用KitKat的2014版本,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();
}
}
Run Code Online (Sandbox Code Playgroud)
请注意,结果可能大于重新使用位图来解码较小尺寸的其他位图或手动重新配置的结果.getAllocationByteCount() getByteCount()
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();
}
}
Run Code Online (Sandbox Code Playgroud)
与@ user289463答案的唯一区别是使用getAllocationByteCount()KitKat及以上版本.
| 归档时间: |
|
| 查看次数: |
61180 次 |
| 最近记录: |