title: ViewPager with ImageView gives "java.lang.OutOfMemoryError: bitmap size exceeds VM budget"
body: <p>I made a <code>ViewPager</code> to display images. When I advance some pages I get an <code>java.lang.OutOfMemoryError: bitmap size exceeds VM budget</code> error. </p>
  
  <p>There are more questions about this issue but I did not find the solution (<code>BitMap.Recycle</code>, <code>System.gc()</code> etc). If you have a suggestion or solution please let me know!</p>
  
  <p>The PNG's are 628KB, 478KB, 587KB, 132KB, 139KB, 149KB, 585KB (crash).</p>
  
  <p>If you have an other solution (scroll images like pages) for me let met know!</p>
  
  <p><strong>My code:</strong></p>
  
  <pre><code>package nl.ipear.vngmagazine;
  import android.content.Context;
  import android.graphics.BitmapFactory;
  import android.os.Bundle;
  import android.os.Environment;
  import android.os.Parcelable;
  import android.support.v4.app.FragmentActivity;
  import android.support.v4.view.PagerAdapter;
  import android.support.v4.view.ViewPager;
  import android.util.Log;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.widget.ImageView;
  
  public class ShowMagazine2 extends FragmentActivity {
      private ViewPager myPager;
      private MyPagerAdapter myPagerAdapter;
  
      private static int NUM_PAGES = 15;
  
      /** Called when the activity is first created. */
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.showmagazine);
  
          myPager = (ViewPager)findViewById(R.id.viewpager1);
          myPagerAdapter = new MyPagerAdapter();
          myPager.setAdapter(myPagerAdapter);
  
          return;
      }
  
      private class MyPagerAdapter extends PagerAdapter{
          @Override
          public int getCount() {
              return NUM_PAGES;
          }
  
          @Override
          public Object instantiateItem(View collection, int position) {      
              String location = Environment.getExternalStorageDirectory() + "/MYData/" + "2012-02/";
  
              // Inflate and create the view
              LayoutInflater layoutInflater = (LayoutInflater) collection.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
              View view = layoutInflater.inflate(R.layout.magazinepageview, null);
  
              ImageView imageView = (ImageView) view.findViewById(R.id.magazinePageImage);
              String fileName = String.format("%s%s%02d%s", location, "2012-02_Page_", position + 1, ".png");
              Log.v("PNG", fileName);
              imageView.setImageBitmap(BitmapFactory.decodeFile(fileName));
  
              ((ViewPager) collection).addView(view,0);
  
              return view;
          }
  
          @Override
          public void destroyItem(View collection, int position, Object view) {
              ((ViewPager) collection).removeView((View) view);           
          }
  
          @Override
          public boolean isViewFromObject(View view, Object object) {
              return view==((View)object);
          }
  
          @Override
          public void finishUpdate(View arg0) {}
  
          @Override
          public void restoreState(Parcelable arg0, ClassLoader arg1) {}
  
          @Override
          public Parcelable saveState() {
              return null;
          }
  
          @Override
          public void startUpdate(View arg0) {}   
      }
  }
  </code></pre>