title: Out of memory error when calling createBitmap
body: <p>I was trying to fix the issue of <a href="https://stackoverflow.com/questions/12933085/android-camera-intent-saving-image-landscape-when-taken-portrait">Android Camera Intent Saving Image Landscape When Taken Portrait</a>, but ran into the issue of an <code>dalvikvm-heap Out of memory on a 63489040-byte allocation.</code> error. I looked at <a href="https://stackoverflow.com/questions/9885647/createbitmap-leads-me-into-a-java-lang-outofmemoryerror">createBitmap() leads me into a java.lang.OutOfMemoryError</a>, but that question was not of any help. I'm not sure how to fix this. I tried calling <code>recycle()</code>on the bitmap, but that didn't work.</p>
  
  <pre><code>String file = getRealPathFromURI(Uri.parse(mUriString));
  BitmapFactory.Options bounds = new BitmapFactory.Options();
  bounds.inJustDecodeBounds = true;
  BitmapFactory.decodeFile(file, bounds);
  
  BitmapFactory.Options opts = new BitmapFactory.Options();
  Bitmap bm = BitmapFactory.decodeFile(file, opts);
  ExifInterface exif = new ExifInterface(file);
  String orientString = exif.getAttribute(ExifInterface.TAG_ORIENTATION);
  int orientation = orientString != null ? Integer.parseInt(orientString) : ExifInterface.ORIENTATION_NORMAL;
  int rotationAngle = 0;
  
  switch(orientation) {
  case ExifInterface.ORIENTATION_ROTATE_90:
      rotationAngle = 90;
  case ExifInterface.ORIENTATION_ROTATE_180:
      rotationAngle = 180;
  case ExifInterface.ORIENTATION_ROTATE_270:
      rotationAngle = 270;
  }
  
  Matrix matrix = new Matrix();
  matrix.setRotate(rotationAngle, (float) bm.getWidth() / 2, (float) bm.getHeight() / 2);
  Bitmap rotatedBitmap = Bitmap.createBitmap(bm, 0, 0, bounds.outWidth, bounds.outHeight, matrix, true);
  bm.recycle();
  </code></pre>
  
  <p>This code reads the EXIF data and orientates the image properly.</p>
  
  <p>I should also add that I am compressing the image here:</p>
  
  <pre><code>ByteArrayOutputStream baos = new ByteArrayOutputStream();
  rotatedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
  byte[] imageBytes = baos.toByteArray();
  rotatedBitmap.recycle();
  baos.close();
  </code></pre>