title: Bitmap Out Of Memory Issues
body: <p><strong>My Problem</strong> </p>
  
  <p>I take a picture with my android device. I then decode that picture from file.</p>
  
  <pre><code>        Bitmap photo = BitmapFactory.decodeFile(EXTERNAL_IMAGE_PATH+File.separator+this._currentPhotoName+JPEG_FILE_SUFFIX);
          if (photo == null &amp;&amp; data != null) 
              photo = (Bitmap) data.getExtras().get("data");
          else if (data == null &amp;&amp; photo == null)
              Log.e("CCPhotoManager","Can't find image from file or from intent data.");
  </code></pre>
  
  <p>I then check that picture and see whether it needs to  be rotated to the correct orientation. </p>
  
  <pre><code>             try {
                  ExifInterface exif = new ExifInterface(EXTERNAL_IMAGE_PATH+File.separator+this._currentPhotoName+JPEG_FILE_SUFFIX);
                  int rotation = CCDataUtils.exifToDegrees(exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,ExifInterface.ORIENTATION_NORMAL));
                  Log.v("CCPhotoManager", "Rotation:"+rotation);
                  if (rotation &gt; 0) {
                      photo = this.convertSavedImageToCorrectOrientation(EXTERNAL_IMAGE_PATH+File.separator+this._currentPhotoName+JPEG_FILE_SUFFIX, photo, rotation);
                  }
              } catch (IOException e1) {
                  // TODO Auto-generated catch block
                  e1.printStackTrace();
              }
  </code></pre>
  
  <p>If it does need rotating I call this method.</p>
  
  <pre><code>public Bitmap convertSavedImageToCorrectOrientation(String filePath,Bitmap photo,int rotation) {
          Log.d("CCPhotoManager", "Changing Orientation of photo located at: "+filePath+" Rotating by:"+rotation);
          int width = photo.getWidth();
          int height = photo.getHeight();
  
  
          Matrix matrix = new Matrix();
          matrix.preRotate(rotation);
  
          Bitmap adjusted = Bitmap.createBitmap(photo, 0, 0, width, height, matrix, true);
  
          try {
                 FileOutputStream out = new FileOutputStream(filePath);
                 adjusted.compress(Bitmap.CompressFormat.JPEG, 100, out);
          } catch (Exception e) {
                 e.printStackTrace();
          }
  
          return adjusted;
      }
  </code></pre>
  
  <p>I am getting <strong>Out of Memory</strong> complaints if the <code>convertSavedImageToCorrectOrientation</code> is called on the line <code>Bitmap adjusted = Bitmap.createBitmap(photo,0,0,width,height,matrix,true);</code></p>
  
  <p>This is only the case on the <code>Samsung Galaxy S3</code>. It works fine on the <code>Samsung Galaxy Ace</code>, <code>HTC Hero</code> and the <code>Sony Xperia U</code>.</p>
  
  <p>Here is the error.</p>
  
  <pre><code>10-17 14:33:33.950: E/AndroidRuntime(12556): java.lang.OutOfMemoryError
  10-17 14:33:33.950: E/AndroidRuntime(12556):    at android.graphics.Bitmap.nativeCreate(Native Method)
  10-17 14:33:33.950: E/AndroidRuntime(12556):    at android.graphics.Bitmap.createBitmap(Bitmap.java:605)
  10-17 14:33:33.950: E/AndroidRuntime(12556):    at android.graphics.Bitmap.createBitmap(Bitmap.java:551)
  </code></pre>
  
  <p>It's a massive amount of memory too.</p>
  
  <pre><code>10-17 14:33:33.945: E/dalvikvm-heap(12556): Out of memory on a 31961104-byte allocation.
  </code></pre>
  
  <p>I think its something to do with the amount of Bitmaps around but I'm not sure how to stop this error from happening.</p>
  
  <p>I know you can call <code>.recycle();</code> on them but it doesn't seem to work.</p>
  
  <p><strong>My Question</strong></p>
  
  <p>How do I correctly handle my Bitmaps so I don't have this OOM problem?</p>
  
  <p><strong>Thanks in advance</strong></p>