内存有效的图像调整大小的Android [英] Memory efficient image resize in Android
问题描述
我想,以减少检索的形式摄像头(SO〜5-8万像素)下降到一节AA几个更小的尺寸(最大的是1024×768)图像的大小。我尝试以下code,但我一直得到一个的OutOfMemoryError
。
I am trying to reduce the size of images retrieved form the camera (so ~5-8 mega pixels) down to one a a few smaller sizes (the largest being 1024x768). I Tried the following code but I consistently get an 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
发生。是否有更多的内存高效的方法来做到这一点?也许一些不要求我整个原始加载到内存中?
It looks like the OOM happens during the createBitmap
. Is there a more memory efficient way to do this? Perhaps something that doesn't require me to load the entire original into memory?
推荐答案
在您下与该BitmapFactory位图德$ C $,传递一个BitmapFactory.Options对象,并指定inSampleSize。这是当对图像进行解码,以节省存储器的最佳方式。
When you decode the bitmap with the BitmapFactory, pass in a BitmapFactory.Options object and specify inSampleSize. This is the best way to save memory when decoding an image.
下面是一个示例code <一个href="http://stackoverflow.com/questions/477572/android-strange-out-of-memory-issue/823966#823966">http://stackoverflow.com/questions/477572/android-strange-out-of-memory-issue/823966#823966
Here's a sample code http://stackoverflow.com/questions/477572/android-strange-out-of-memory-issue/823966#823966
这篇关于内存有效的图像调整大小的Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!