title: OutOfMemory while bitmap decodeByteArray
body: <p>I am trying to load a byte array into a bitmap. I already tried to downscale the image so it will be smaller but I still get an OutOfMemory error. How can I make sure that people don't get an OutOfMemory error here?</p>
  
  <p>The code I am using to create the bitmap:</p>
  
  <pre><code>private void rotatePicture(int rotation, byte[] data, ImageView photoImageView) {
      BitmapFactory.Options opts = new BitmapFactory.Options();
      opts.inSampleSize = 2; // Power of 2
      Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length, opts);
  ...
  </code></pre>
  
  <p>The stacktrace:</p>
  
  <pre><code>09-15 11:09:28.182  11831-11831/com.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
      java.lang.OutOfMemoryError
              at android.graphics.BitmapFactory.nativeDecodeByteArray(Native Method)
              at android.graphics.BitmapFactory.decodeByteArray(BitmapFactory.java:522)
              at com.app.SquareCamera.EditSavePhotoFragment.rotatePicture(EditSavePhotoFragment.java:85)
              at com.app.SquareCamera.EditSavePhotoFragment.onViewCreated(EditSavePhotoFragment.java:72)
              at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:973)
              at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138)
              at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:740)
              at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501)
              at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:458)
              at android.os.Handler.handleCallback(Handler.java:730)
              at android.os.Handler.dispatchMessage(Handler.java:92)
              at android.os.Looper.loop(Looper.java:176)
              at android.app.ActivityThread.main(ActivityThread.java:5419)
              at java.lang.reflect.Method.invokeNative(Native Method)
              at java.lang.reflect.Method.invoke(Method.java:525)
              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
              at dalvik.system.NativeStart.main(Native Method)
  </code></pre>
  
  <p><strong>Edit:</strong></p>
  
  <p>I now tried to use the examples from <a href="http://developer.android.com/training/displaying-bitmaps/load-bitmap.html" rel="nofollow">http://developer.android.com/training/displaying-bitmaps/load-bitmap.html</a> using the following code. But I still get an OutOfMemory error on <code>
          return BitmapFactory.decodeByteArray(data, 0, data.length, options);</code> in the <code>decodeSampledBitmapFromByteArray</code> method.</p>
  
  <pre><code>private void rotatePicture(int rotation, byte[] data, ImageView photoImageView) {
      BitmapFactory.Options options = new BitmapFactory.Options();
      options.inJustDecodeBounds = true;
      BitmapFactory.decodeByteArray(data, 0, data.length, options);
      int imageHeight = options.outHeight;
      int imageWidth = options.outWidth;
  
      Bitmap bitmap = decodeSampledBitmapFromByteArray(data, imageWidth, imageHeight);
      ...
  }
  
  
  public static Bitmap decodeSampledBitmapFromByteArray(byte[] data,
                                                       int reqWidth, int reqHeight) {
  
      // First decode with inJustDecodeBounds=true to check dimensions
      final BitmapFactory.Options options = new BitmapFactory.Options();
      options.inJustDecodeBounds = true;
      BitmapFactory.decodeByteArray(data, 0, data.length, options);
  
      // Calculate inSampleSize
      options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
  
      // Decode bitmap with inSampleSize set
      options.inJustDecodeBounds = false;
      return BitmapFactory.decodeByteArray(data, 0, data.length, options);
  }
  
  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;
  
      if (height &gt; reqHeight || width &gt; reqWidth) {
  
          final int halfHeight = height / 2;
          final int halfWidth = width / 2;
  
          // Calculate the largest inSampleSize value that is a power of 2 and keeps both
          // height and width larger than the requested height and width.
          while ((halfHeight / inSampleSize) &gt; reqHeight
                  &amp;&amp; (halfWidth / inSampleSize) &gt; reqWidth) {
              inSampleSize *= 2;
          }
      }
  
      return inSampleSize;
  }
  </code></pre>