【发布时间】:2012-08-09 22:34:10
【问题描述】:
Android 位图大小超出 VM 预算。
我的应用经常出现此错误。我有两个问题。
- 我是否需要回收我的 about 活动(它包含一些图像视图和按钮以及文本视图)?
-
.recycle();和system.gc();有什么区别?
【问题讨论】:
Android 位图大小超出 VM 预算。
我的应用经常出现此错误。我有两个问题。
.recycle(); 和system.gc(); 有什么区别?【问题讨论】:
试试看这个视频(罗马人):
http://www.youtube.com/watch?v=duefsFTJXzc&list=PLD1B287286E23E2D1&index=1&feature=plpp_video
它将提供一些关于位图最佳实践的见解。
【讨论】:
你应该总是尝试recycle位图在你使用它们之后。
据我了解,您应该尽量避免致电system.gc()。
调用recycle() 将允许对位图对象进行垃圾回收。
我希望这会有所帮助。
【讨论】:
我在从相机中挑选图像时遇到了同样的问题。
我使用以下代码调整了图像位图的大小:
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;
}
【讨论】: