Android Studio bitmap allocation out-of-memory error
<p>I'm using this function to rotate a bitmap from the camera or the gallery:</p>

<pre><code>public static Bitmap fixOrientation(Bitmap mBitmap) {

    if (mBitmap.getWidth() &gt; mBitmap.getHeight()) {
        Matrix matrix = new Matrix();
        matrix.postRotate(90);
        return Bitmap.createBitmap(mBitmap , 0, 0, mBitmap.getWidth(), mBitmap.getHeight(), matrix, true); // the error is here!
    }
    return mBitmap;
}
</code></pre>

<p>Its workes fine in the first two times i'm using it, but in the third time it crashes the app and giving me this error:</p>

<pre><code>java.lang.OutOfMemoryError: Failed to allocate a 36578316 byte allocation with 16771872 free bytes and 29MB until OOM
</code></pre>

<p>This is where this function is called:</p>

<pre><code>    @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (resultCode == RESULT_OK &amp;&amp; data != null) {

        Uri uri = data.getData();

        try {

            Bitmap sourceBitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri);

            Bitmap correctBitmap = fixOrientation(sourceBitmap);
            image.setImageBitmap(correctBitmap);

            bitmapsArray[cameraSideInt] = correctBitmap;
            chooseImageLayout.setVisibility(View.GONE);
            // show change layout

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
</code></pre>

<p>Can anyone Think of a way to solve this error?</p>