我正在尝试将从相机中检索到的图像(大约5-8百万像素)缩小到一个较小的尺寸(最大为1024x768).我尝试了以下代码,但我一直得到一个OutOfMemoryError.
Bitmap image = BitmapFactory.decodeStream(this.image, null, opt);
int imgWidth = image.getWidth();
int imgHeight = image.getHeight();
// Constrain to given size but keep aspect ratio
float scaleFactor = Math.min(((float) width) / imgWidth, ((float) height) / imgHeight);
Matrix scale = new Matrix();
scale.postScale(scaleFactor, scaleFactor);
final Bitmap scaledImage = Bitmap.createBitmap(image, 0, 0, imgWidth, imgHeight, scale, false);
image.recycle();
看起来OOM发生在createBitmap.是否有更高效的内存方式?也许某些东西不需要我将整个原件加载到内存中?