Android位图大小超过虚拟机预算。Android位图大小超过虚拟机预算错误
我的应用程序经常收到此错误。我有两个问题。
- 我是否需要回收我的关于活动(它包含一些imageviews和按钮和textViews)?
.recycle();
和system.gc();
之间的区别是什么?
Android位图大小超过虚拟机预算。Android位图大小超过虚拟机预算错误
我的应用程序经常收到此错误。我有两个问题。
.recycle();
和system.gc();
之间的区别是什么?你应该总是尝试和recycle
你已经使用它们的位图。
据我了解,您应该尽量避免拨打system.gc()
。 调用recycle()
将允许垃圾收集位图对象。
我希望这会有所帮助。
非常感谢你=) – 2012-08-13 17:10:50
我做了一个新的帖子,更好的信息http://stackoverflow.com/questions/11942707/android-bitmap-size-exceeds-vm-budget-when-dealing-与位图 – 2012-08-13 21:42:41
尝试观看这部影片(罗曼盖伊):
http://www.youtube.com/watch?v=duefsFTJXzc&list=PLD1B287286E23E2D1&index=1&feature=plpp_video
它将提供一些见解位图的最佳实践。
从相机中挑选图像时出现同样的问题。
我用下面的代码调整图像的位图:
Bitmap bitmap = resizeBitMapImage(picturePath, 75, 91);
profilePic.setImageBitmap(bitmap);
private Bitmap resizeBitMapImage(String filePath, int targetWidth,
int targetHeight) {
Bitmap bitMapImage = null;
// First, get the dimensions of the image
Options options = new Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath, options);
double sampleSize = 0;
// Only scale if we need to
// (16384 buffer for img processing)
Boolean scaleByHeight = Math.abs(options.outHeight - targetHeight) >= Math
.abs(options.outWidth - targetWidth);
if (options.outHeight * options.outWidth * 2 >= 1638) {
// Load, scaling to smallest power of 2 that'll get it <= desired
// dimensions
sampleSize = scaleByHeight ? options.outHeight/targetHeight
: options.outWidth/targetWidth;
sampleSize = (int) Math.pow(2d,
Math.floor(Math.log(sampleSize)/Math.log(2d)));
}
// Do the actual decoding
options.inJustDecodeBounds = false;
options.inTempStorage = new byte[128];
while (true) {
try {
options.inSampleSize = (int) sampleSize;
bitMapImage = BitmapFactory.decodeFile(filePath, options);
break;
} catch (Exception ex) {
try {
sampleSize = sampleSize * 2;
} catch (Exception ex1) {
}
}
}
return bitMapImage;
}
参考此答案http://stackoverflow.com/questions/11373240/android-error-java-lang-outofmemoryerror-bitmap-size-exceeds-vm - 预算/ 11373278#11373278和这到http://stackoverflow.com/questions/3823799/android-bitmap-recycle-how-does-it-work – Aerrow 2012-08-13 16:58:25