在Android中缩放图像会导致OutOfMemory异常
|
我正在开发一个包含许多图片作为精灵的Android游戏。
当我以下列方式加载图像时:
public static Bitmap loadBitmap(int resId) {
return BitmapFactory.decodeResource(getResources(), resId, options);
}
一切正常。
当我尝试使用以下代码缩小或放大位图时:
public static Bitmap loadBitmap(int resId) {
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), resId, options);
Matrix matrix = new Matrix();
matrix.postScale(0.8f, 0.8f);
Bitmap scaledBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
bitmap.recycle();
bitmap = null;
return scaledBitmap;
}
该应用程序崩溃,但出现以下异常:
2211840-byte external allocation too large for this process.
Out of memory: Heap Size=4935KB, Allocated=2549KB, Bitmap Size=18463KB
VM won\'t let us allocate 2211840 bytes
为什么缩放导致OutOfMemory异常?我什至尝试回收原始图像以节省空间。我不是故意使用Bitmap.createScaledBitmap(...)
,因为此方法确实会在内部发生内存泄漏(如其他在线资源中所述)。
先感谢您,
兹拉特科
没有找到相关结果
已邀请:
3 个回复
浆错
阀傻
搁手