title: How to avoid OOM to load large images from android resource
body: <p>I have developed 20+ android apps, apps have a tutorial activity that are composed of large image slide (ex; 4 images in the size of 750 x 1334) and would be shown to users at the first launch.</p>
  
  <p>I cannot reduce the sizes anymore, because of image quality.</p>
  
  <p>My code snippet is following;</p>
  
  <pre><code>public class GalleryImageActivity extends BaseActivity implements OnGestureListener, OnTouchListener {
  ViewPager imagePager;
  GalleryImageAdapter galleryImageAdapter;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.gallery_image);
  
      imagePager = (ViewPager) findViewById(R.id.image_pager);
  
      galleryImageAdapter = new GalleryImageAdapter(this);
      imagePager.setAdapter(galleryImageAdapter);
      imagePager.setOnTouchListener(this);
  }
  }
  
  
  public class GalleryImageAdapter extends PagerAdapter {
  public static int SCREEN_CNT = 4;
  Bitmap[] bmp = new Bitmap[SCREEN_CNT];
  
  @Override
  public int getCount() {
      return SCREEN_CNT;
  }
  
  @Override
  public Object instantiateItem(ViewGroup view, int position) {
      View view = inflater.inflate(R.layout.gallery_image_item, null);
      ImageView imv = (ImageView) view.findViewById(R.id.imgView);
  
      bmp[position] = readBitMap(context, R.drawable.tutorial_img_0 + position, position); 
      if (bmp[position] != null)
          imv.setImageBitmap(bmp[position]);
  }
  
  @Override
  public void destroyItem(ViewGroup container, int position, Object object) {
  
      if (bmp[position] != null &amp;&amp; !bmp[position].isRecycled())
      {
          bmp[position].recycle();   
          System.gc();  
          bmp[position] = null;
      }
  }
  
  public Bitmap readBitMap(Context context, int resId, int position) { 
      BitmapFactory.Options opt = new BitmapFactory.Options();
      opt.inJustDecodeBounds = true;
  
      InputStream is = context.getResources().openRawResource(resId);
          BitmapFactory.decodeStream(is,null, opt);
      opt.inPreferredConfig = Bitmap.Config.RGB_565;
      opt.inPurgeable = true;
      opt.inInputShareable = true;
      opt.inJustDecodeBounds = false;
  
      try {
          is.close();
      } catch (IOException e) {                
          e.printStackTrace();
      }
  
      is = context.getResources().openRawResource(resId);
      Bitmap bitmap = BitmapFactory.decodeStream(is,null, opt);      
  
      try {
          is.close();
      } catch (IOException e) {                
          e.printStackTrace();
      }            
  
      return bitmap;
  }
  }
  </code></pre>
  
  <p>sometimes it works well, but when repeat 5~6 times, BitmapFactory.decodeStream throws "Out of memory".
  How can i decide scale size or resolve this problem?</p>
  
  <p>the example images are following;
  <a href="https://i.stack.imgur.com/k0Axu.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/k0Axu.png" alt="enter image description here"></a></p>