OutOfMemory Exception when handling images
<blockquote>
  <p><strong>Possible Duplicate:</strong><br>
  <a href="http://stackoverflow.com/questions/2928002/outofmemoryerror-bitmap-size-exceeds-vm-budget-android">OutOfMemoryError: bitmap size exceeds VM budget :- Android</a>  </p>
</blockquote>



<p>Im in the process of writing a program which uses images from the gallery and then displays them in an activity (one image pr. activity). However I've been bumping into this error over and over again for three days straight without doing any progress of eliminating it:</p>

<pre><code>07-25 11:43:36.197: ERROR/AndroidRuntime(346): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
</code></pre>

<p>My code flow is as follows:</p>

<p>When the user presses a button an intent is fired leading to the gallery:</p>

<pre><code> Intent galleryIntent = new Intent(Intent.ACTION_GET_CONTENT);
 galleryIntent.setType("image/*");
 startActivityForResult(galleryIntent, 0);
</code></pre>

<p>Once the user has selected an image the image is presented in an imageview:</p>

<pre><code>  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     android:orientation="vertical"&gt;

&lt;ImageView
    android:background="#ffffffff"
    android:id="@+id/image"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_gravity="center"
    android:maxWidth="250dip"
    android:maxHeight="250dip"
    android:adjustViewBounds="true"/&gt;

 &lt;/LinearLayout&gt;
</code></pre>

<p>In the onActivityResult  method i have:</p>

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

    if(resultCode == RESULT_OK) {
        switch(requestCode) {
        case 0:             // Gallery
            String realPath = getRealPathFromURI(data.getData());
            File imgFile = new File(realPath);
            Bitmap myBitmap;
            try {
                myBitmap = decodeFile(imgFile);
                Bitmap rotatedBitmap = resolveOrientation(myBitmap);
                img.setImageBitmap(rotatedBitmap);
                OPTIONS_TYPE = 1;
            } catch (IOException e) { e.printStackTrace(); }

            insertImageInDB(realPath);

            break;
        case 1:             // Camera
</code></pre>

<p>The decodeFile method is from <a href="http://stackoverflow.com/questions/477572/android-strange-out-of-memory-issue/823966#823966">here</a> and the resolveOrientation method just wraps the bitmap into a matrix and turns it 90 degrees clockwise.</p>

<p>I really hope someone can help me resolve this matter. </p>
