title: Showing large images on view pager from sdcard in android
body: <p>I have a problem that I have 80 to 150 images of very High Resolution and I have to show them in a view pager. I had written the simple mechanism of decoding but get OutofMemory Error after the 2nd Page. I tried very much but unable to find the exact solution to avoid that. My own suggestion is that if we will able to load a image in parts on an Image View then perhaps we avoid this but I don't know how to achieve this. Please suggest any solution regarding this.</p>
  
  <p>Code: </p>
  
  <pre><code>private class MyPagerAdapter extends PagerAdapter {
  
          @Override
          public int getCount() {
              return Integer.parseInt(pagesCount);
          }
  
  
          @Override
          public boolean isViewFromObject(View arg0, Object arg1) {
              return arg0==((ImageView)arg1);
          }
          @Override
          public void destroyItem(View container, int position, Object object) {
               View view = (View)object;
                  ((ViewPager) container).removeView(view);
                  view = null;
          }
          @Override
          public View instantiateItem(View container, final int position) {
              final ImageView imgPage = new ImageView(getApplicationContext());
              /*imgPage.setImageURI(Uri.withAppendedPath(
                      Uri.parse(Environment.getExternalStorageDirectory()+"/MAGZ/"+magName+issueName), "" + (issueName+position)+".png"));*/
              //imgPage.setImageBitmap(BitmapFactory.decodeFile(Environment.getExternalStorageDirectory()+"/MAGZ/"+magName+issueName+"/"+(issueName+position)));
              //destroyItem(container, position, imgPage);
              imgPage.setScaleType(android.widget.ImageView.ScaleType.FIT_XY);
              imgPage.setImageDrawable(getImageFromSdCard(issueName+position));
              //imgPage.setImageResource(getImageFromSdCard(issueName+position));
  
  
              ((ViewPager) container).addView(imgPage,0); 
              return imgPage;
          }
      }
  
  
      public Drawable getImageFromSdCard(String imageName) {
          Drawable d = null;
          try {
              String path = Environment.getExternalStorageDirectory()+"/MAGZ/"+magName+"/"+magName+issueName;
              bitmap = BitmapFactory.decodeFile(path + "/" + imageName
                      + ".png");
  
             // d = new BitmapDrawable(bitmap);
              d = new BitmapDrawable(decodeFile(new File(path+"/"+imageName+".png")));
             // bitmap.recycle();
              bitmap = null;
          } catch (IllegalArgumentException e) {
              bitmap = null;
          }
          return d;
  
      }
  
  
      //decodes image and scales it to reduce memory consumption
      private Bitmap decodeFile(File f){
          try {
              //Decode image size
              BitmapFactory.Options o = new BitmapFactory.Options();
              o.inJustDecodeBounds = true;
              BitmapFactory.decodeStream(new FileInputStream(f),null,o);
  
              //The new size we want to scale to
              final int REQUIRED_SIZE=550;
  
              //Find the correct scale value. It should be the power of 2.
              int scale=1;
              while(o.outWidth/scale/2&gt;=REQUIRED_SIZE &amp;&amp; o.outHeight/scale/2&gt;=REQUIRED_SIZE)
                  scale*=2;
  
              //Decode with inSampleSize
              BitmapFactory.Options o2 = new BitmapFactory.Options();
              o2.inSampleSize=scale;
              return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
          } catch (FileNotFoundException e) {}
          return null;
      }
  </code></pre>
  
  <p>Thanks in advance.</p>