我正在尝试将从相机中检索到的图像的大小(大约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();看起来像是在createBitmap过程中发生了对象映射。有没有更有效利用内存的方法呢?也许是一些不需要我将整个原始文件加载到内存中的东西?
发布于 2010-07-11 04:50:08
当您使用BitmapFactory解码位图时,传入一个BitmapFactory.Options对象并指定inSampleSize。这是在解码图像时节省内存的最好方法。
下面是一个示例代码Strange out of memory issue while loading an image to a Bitmap object
发布于 2010-07-10 23:42:14
您是否在应用程序中使用相机拍照?如果是这样的话,当通过android.hardware.Camera.Parameters.setPictureSize捕获图片时,首先应该将图片大小设置为较小的大小
发布于 2015-04-21 07:26:45
我回答了一个类似的问题,并展示了如何动态加载合适的图像大小。
https://stackoverflow.com/questions/3219672
复制相似问题