title: Scaling images in Android causes OutOfMemory exception
body: <p>I'm developing an Android game consisting of many images as sprites.</p>
  
  <p>When I load the images the following way:</p>
  
  <pre><code>public static Bitmap loadBitmap(int resId) {  
      return BitmapFactory.decodeResource(getResources(), resId, options);
  }
  </code></pre>
  
  <p>everything works perfectly fine.</p>
  
  <p>When I try to down-scale or up-scale the bitmap with this code:</p>
  
  <pre><code>public static Bitmap loadBitmap(int resId) {
      Bitmap bitmap = BitmapFactory.decodeResource(getResources(), resId, options);
      Matrix matrix = new Matrix();
      matrix.postScale(0.8f, 0.8f);
      Bitmap scaledBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
      bitmap.recycle();
      bitmap = null;
      return scaledBitmap;
  }
  </code></pre>
  
  <p>the application crashes with the following exception:</p>
  
  <pre><code>2211840-byte external allocation too large for this process.  
  Out of memory: Heap Size=4935KB, Allocated=2549KB, Bitmap Size=18463KB  
  VM won't let us allocate 2211840 bytes
  </code></pre>
  
  <p>Why is the scaling causing OutOfMemory exception? I even try to recycle the original image in order to save some space. I'm not using <code>Bitmap.createScaledBitmap(...)</code> intentionally, since this method does memory leaking internally (as explained in other online resources).</p>
  
  <p>Thank you in advance,<br>
  Zlatko</p>