Android 解码后位图字节大小?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2407565/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Bitmap byte-size after decoding?
提问by user289463
How can I determine/calculate the byte size of a bitmap (after decoding with BitmapFactory)? I need to know how much memory space it occupies, because I'm doing memory caching/management in my app. (file size is not enough, since these are jpg/png files)
如何确定/计算位图的字节大小(使用 BitmapFactory 解码后)?我需要知道它占用了多少内存空间,因为我正在我的应用程序中进行内存缓存/管理。(文件大小不够,因为这些是 jpg/png 文件)
Thanks for any solutions!
感谢您提供任何解决方案!
Update: getRowBytes * getHeight might do the trick.. I'll implement it this way until someone comes up with something against it.
更新: getRowBytes * getHeight 可能会起作用.. 我会以这种方式实现它,直到有人提出反对它。
回答by user289463
getRowBytes() * getHeight()
seems to be working fine to me.
getRowBytes() * getHeight()
对我来说似乎工作正常。
Update to my ~2 year old answer: Since API level 12 Bitmap has a direct way to query the byte size: http://developer.android.com/reference/android/graphics/Bitmap.html#getByteCount%28%29
更新我大约 2 岁的答案:由于 API 级别 12 位图有查询字节大小的直接方法:http: //developer.android.com/reference/android/graphics/Bitmap.html#getByteCount%28%29
----Sample code
----示例代码
@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();
}
}
回答by android developer
It's best to just use the support library:
最好只使用支持库:
int bitmapByteCount=BitmapCompat.getAllocationByteCount(bitmap)
But if you have the Android project to use at least minSdk of 19 (kitkat, meaning 4.4), you can just use bitmap.getAllocationByteCount().
但是如果你有 Android 项目至少使用 19 的 minSdk(kitkat,意思是 4.4),你可以只使用bitmap.getAllocationByteCount()。
回答by Patrick Favre
Here is the 2014 version that utilizes KitKat's getAllocationByteCount()
and is written so that the compiler understands the version logic (so @TargetApi
is not needed)
这是使用 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();
}
}
Note that the result of getAllocationByteCount()
canbe larger than the result of getByteCount()
if a bitmap is reused to decode other bitmaps of smaller size, or by manual reconfiguration.
请注意,如果位图被重用于解码其他较小尺寸的位图,或者通过手动重新配置,则结果可能大于结果。getAllocationByteCount()
getByteCount()
回答by ahmed_khan_89
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();
}
}
The only difference with @user289463 answer, is the use of getAllocationByteCount()
for KitKat and above versions.
与@user289463 答案的唯一区别是使用getAllocationByteCount()
KitKat 及以上版本。