title: Memory efficient image resize in Android
body: <p>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 <code>OutOfMemoryError</code>.</p>
  
  <pre><code>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();
  </code></pre>
  
  <p>It looks like the OOM happens during the <code>createBitmap</code>. Is there a more memory efficient way to do this? Perhaps something that doesn't require me to load the entire original into memory?</p>