2010-07-10 35 views
14

我试图将从相机(大约5-8百万像素)检索到的图像缩小到一个较小的大小(最大为1024x768)。我试过下面的代码,但我始终得到一个OutOfMemoryErrorAndroid中的内存高效图像大小调整

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期间。有没有更高效的内存方式来做到这一点?也许有些东西不需要我将整个原件加载到内存中?

回答

1

您是否在应用程序中使用相机拍摄照片?如果是这样,您应该将图片大小设置为较小的一个,当他们被捕获时通过android.hardware.Camera.Parameters.setPictureSize

+0

不,我现在用的'android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI'让用户选择图像。 – Sionide21 2010-07-10 15:46:37