title: Android out of memory exception with bitmaps
body: <p>First off, I have read many posts and articles about out of memory exceptions but none of them have helped with my situation.  What I'm trying to do is load an image from the sd card but scale it to an exact pixel size.  </p>
  
  <p>I first get the width and height of the image and calculate the sample size:</p>
  
  <pre><code>    final BitmapFactory.Options options = new BitmapFactory.Options();
      options.inJustDecodeBounds = true;
      BitmapFactory.decodeFile(backgroundPath, options);
  
      // Calculate inSampleSize
      options.inSampleSize = calculateInSampleSize(options, getWidth(), getHeight());
  </code></pre>
  
  <p>Here's how I get the sample size (although its not really relevant):</p>
  
  <pre><code>public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
  
      // Raw height and width of image
      final int height = options.outHeight;
      final int width = options.outWidth;
      int inSampleSize = 1;
  
      // NOTE: we could use Math.floor here for potential better image quality
      // however, this also results in more out of memory issues
      if (height &gt; reqHeight || width &gt; reqWidth) {
          if (width &gt; height) {
              inSampleSize = Math.round((float)height / (float)reqHeight);
          } else {
              inSampleSize = Math.round((float)width / (float)reqWidth);
          }
      }
  
      return inSampleSize;
  }
  </code></pre>
  
  <p>Now that I have a sample size I load the image from disk to an approximate size (sample size):</p>
  
  <pre><code>    // Decode bitmap with inSampleSize set
      options.inJustDecodeBounds = false;
      options.inPurgeable = true;
      Bitmap bmp = BitmapFactory.decodeFile(backgroundPath, options);
  </code></pre>
  
  <p>Now, I scale this bitmap that I have created to the exact size I need and clean up:</p>
  
  <pre><code>    // scale the bitmap to the exact size we need
      Bitmap editedBmp = Bitmap.createScaledBitmap(bmp, (int) (width * scaleFactor), (int) (height * scaleFactor), true); 
  
      // clean up first bitmap
      bmp.recycle();
      bmp = null;
      System.gc();    // I know you shouldnt do this, but I'm desperate 
  </code></pre>
  
  <p>The above step is usually get my out of memory exception.  Does anyone know a way to load an exact size bitmap from disk to avoid having to create two separate bitmaps like above?</p>
  
  <p>Also, it seems like more exceptions occur when the user runs this code for a second time (sets a new image).  However, I make sure to unload the drawable that was created from the bitmap which allows it to be garbage collected before this code is run again.</p>
  
  <p>Any suggestions?</p>
  
  <p>Thanks,
  Nick</p>