title: Android: java.lang.OutOfMemoryError: bitmap size exceeds VM budget
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/1949066/java-lang-outofmemoryerror-bitmap-size-exceeds-vm-budget-android">java.lang.OutOfMemoryError: bitmap size exceeds VM budget - Android</a>  </p>
  </blockquote>
  
  
  
  <p>I'm currently developing an Android app that displays images downloaded from URLs and put them in a ListView. And when it's clicked, creates a new ImageView of the image clicked. When smaller images (7kb) are clicked, it runs fine. Even runs fine up to 200kb. But anything larger than that, I get an error "The application has stopped unexpectedly. Please try again.". Looking at LogCat, it seems the problem has to do with Android running out of memory. Is there a solution to this? Or is it just a matter of having to lower the resolution of my images down to the 200kb range? Surely the image size shouldn't matter much up to 1 or 2 megabytes? It may take a bit longer to load but don't see why it would throw an error. </p>
  
  <p>Any help would be much appreciated!</p>
  
  <p>Zoom.java</p>
  
  <pre><code>public class Zoom extends Activity {
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          requestWindowFeature(Window.FEATURE_NO_TITLE);
          getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
          setContentView(R.layout.gallery_zoom);
  
          String selection = getIntent().getExtras().getString("image");
          Toast.makeText(this, selection, Toast.LENGTH_LONG).show();
  
          new backgroundLoader().execute();
      }
  
  
      public class backgroundLoader extends AsyncTask&lt;Void, Void, Void&gt; {
          Bitmap bmp;
  
          @Override
          protected Void doInBackground(Void... params) {
              BitmapFactory.Options bmpOptions;
              bmpOptions = new BitmapFactory.Options();
              bmpOptions.inSampleSize = 1;
              bmp = LoadImage(getIntent().getExtras().getString("image"), bmpOptions);
              return null;
          }
  
          @Override
          protected void onPostExecute(Void result) {
              ImageView image = (ImageView) findViewById(R.id.imageZoom);
              image.setImageBitmap(bmp);
              super.onPostExecute(result);
          }
  
      }
  
      private Bitmap LoadImage(String URL, BitmapFactory.Options options) {
          Bitmap bitmap = null;
          InputStream in = null;
          try {
              in = OpenHttpConnection(URL);
              bitmap = BitmapFactory.decodeStream(in, null, options);
              in.close();
          } catch (IOException e1) {
          }
          return bitmap;
  
      }
  
      private InputStream OpenHttpConnection(String strURL) throws IOException {
          InputStream inputStream = null;
          URL url = new URL(strURL);
  
          HttpURLConnection connection = (HttpURLConnection) url.openConnection();
          try {
              InputStream in = new BufferedInputStream(connection.getInputStream());
              return in;
          } catch (Exception exception) {
              exception.printStackTrace();
              return null;
          }
      }
  
  }
  </code></pre>
  
  <p>LogCat Logs</p>
  
  <pre><code>08-13 07:35:01.798: E/dalvikvm-heap(1132): 48064000-byte external allocation too large for this process.
  08-13 07:35:01.888: E/GraphicsJNI(1132): VM won't let us allocate 48064000 bytes
  08-13 07:35:01.888: D/dalvikvm(1132): GC_FOR_MALLOC freed 1K, 54% free 2694K/5831K, external 2229K/3423K, paused 19ms
  08-13 07:35:01.888: D/skia(1132): --- decoder-&gt;decode returned false
  08-13 07:35:01.888: W/dalvikvm(1132): threadid=14: thread exiting with uncaught exception (group=0x40015560)
  08-13 07:35:01.898: E/AndroidRuntime(1132): FATAL EXCEPTION: AsyncTask #1
  08-13 07:35:01.898: E/AndroidRuntime(1132): java.lang.RuntimeException: An error occured while executing doInBackground()
  08-13 07:35:01.898: E/AndroidRuntime(1132):     at android.os.AsyncTask$3.done(AsyncTask.java:200)
  08-13 07:35:01.898: E/AndroidRuntime(1132):     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
  08-13 07:35:01.898: E/AndroidRuntime(1132):     at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
  08-13 07:35:01.898: E/AndroidRuntime(1132):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
  08-13 07:35:01.898: E/AndroidRuntime(1132):     at java.util.concurrent.FutureTask.run(FutureTask.java:138)
  08-13 07:35:01.898: E/AndroidRuntime(1132):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
  08-13 07:35:01.898: E/AndroidRuntime(1132):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
  08-13 07:35:01.898: E/AndroidRuntime(1132):     at java.lang.Thread.run(Thread.java:1019)
  08-13 07:35:01.898: E/AndroidRuntime(1132): Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget
  08-13 07:35:01.898: E/AndroidRuntime(1132):     at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
  08-13 07:35:01.898: E/AndroidRuntime(1132):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:470)
  08-13 07:35:01.898: E/AndroidRuntime(1132):     at com.example.example.mobile.application.gallery.Zoom.LoadImage(Zoom.java:72)
  08-13 07:35:01.898: E/AndroidRuntime(1132):     at com.example.example.mobile.application.gallery.Zoom.access$0(Zoom.java:67)
  08-13 07:35:01.898: E/AndroidRuntime(1132):     at com.example.example.mobile.application.gallery.Zoom$backgroundLoader.doInBackground(Zoom.java:54)
  08-13 07:35:01.898: E/AndroidRuntime(1132):     at com.example.example.mobile.application.gallery.Zoom$backgroundLoader.doInBackground(Zoom.java:1)
  08-13 07:35:01.898: E/AndroidRuntime(1132):     at android.os.AsyncTask$2.call(AsyncTask.java:185)
  08-13 07:35:01.898: E/AndroidRuntime(1132):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
  08-13 07:35:01.898: E/AndroidRuntime(1132):     ... 4 more
  08-13 07:35:01.908: W/ActivityManager(61):   Force finishing activity com.example.example.mobile.application/.gallery.Zoom
  </code></pre>