title: Android - Saving photo taken from Camera , Out of memory
body: <p>I have a use case where the user takes a picture and the picture has to be saved on sd card .</p>
  
  <p>I have set the orientation of the camera as portrait .</p>
  
  <p>While s<code>aving the photo on to SD card</code> i am getting <code>out of memory</code> .</p>
  
  <p>Here is the log - </p>
  
  <pre><code>java.lang.RuntimeException: An error occured while executing doInBackground()
    at android.os.AsyncTask$3.done(AsyncTask.java:300)
    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
    at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
    at java.util.concurrent.FutureTask.run(FutureTask.java:242)
    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
    at java.lang.Thread.run(Thread.java:841)
  Caused by: java.lang.OutOfMemoryError
    at android.graphics.Bitmap.nativeCreate(Native Method)
    at android.graphics.Bitmap.createBitmap(Bitmap.java:809)
    at android.graphics.Bitmap.createBitmap(Bitmap.java:786)
    at android.graphics.Bitmap.createBitmap(Bitmap.java:718)
    at com.philips.cl.di.haircare.util.AppUtility.changeOrientationAndwriteDataToFile(AppUtility.java:382)
    at com.philips.cl.di.haircare.mirror.MirrorCameraViewFragment$SaveBitMap.doInBackground(MirrorCameraViewFragment.java:389)
    at com.philips.cl.di.haircare.mirror.MirrorCameraViewFragment$SaveBitMap.doInBackground(MirrorCameraViewFragment.java:369)
    at android.os.AsyncTask$2.call(AsyncTask.java:288)
    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
    ... 4 more
  </code></pre>
  
  <p>Here is the function that i am using to save - </p>
  
  <pre><code>public static boolean changeOrientationAndwriteDataToFile(Context context,
          final File pictureFile, final byte[] data, final int camId)
          throws Exception {
  
      FileOutputStream fos = null;
      Bitmap realImage = null;
      try {
  
          fos = new FileOutputStream(pictureFile);
          realImage = BitmapFactory.decodeByteArray(data, 0, data.length);
          android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo();
          android.hardware.Camera.getCameraInfo(camId, info);
          Matrix matrix = new Matrix();
  
          int rotation = info.orientation;
          int camfacing = info.facing;
  
          if (camfacing == 1) {
              matrix.setScale(1, -1);
          }
          if (rotation != 0) {
              matrix.postRotate(rotation);
  
          } else {
              matrix.postRotate(-90);
          }
          realImage = Bitmap.createBitmap(realImage, 0, 0,
                  realImage.getWidth(), realImage.getHeight(), matrix, false);
          MediaScannerConnection.scanFile(context,
                  new String[] { pictureFile.getPath() },
                  new String[] { "image/jpeg" }, null);
  
          // 100 means maximum quality
          return realImage.compress(Bitmap.CompressFormat.JPEG, 100, fos);
      } catch (final Exception e) {
          throw e;
  
      } finally {
          if (realImage != null) {
              realImage.recycle();
          }
          if (fos != null) {
              fos.close();
          }
      }
  
  }
  </code></pre>
  
  <p>Line number - 382 is going out of memory .</p>
  
  <pre><code>realImage = Bitmap.createBitmap(realImage, 0, 0,
                  realImage.getWidth(), realImage.getHeight(), matrix, false);
  </code></pre>
  
  <p>Please help on how to resolve this .</p>
  
  <p>Thanks.</p>