【发布时间】:2023-03-25 08:43:01
【问题描述】:
我正在尝试将从相机检索到的图像的大小(大约 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 期间。有没有更高效的内存方法来做到这一点?也许不需要我将整个原件加载到内存中?
【问题讨论】:
标签: android bitmap image-manipulation