2010-07-10 72 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