title: android: showing 156 images using gridview inside viewpager
body: <p>I would like to show 156 thumbails using Viewpager of 18 pages, with each page having 9 thumbnails. I have implemented as follows:</p>
  
  <h2>Index_gridview class:</h2>
  
  <pre><code>public void init() 
  {  
      PageCount = (int) Math.ceil(mThumbIds.length / PAGE_SIZE);  
      mLists = new ArrayList&lt;GridView&gt;();  
  
      for (int i = 0; i &lt; PageCount; i++) 
      {  
          GridView gv = new GridView(this);  
          gv.setAdapter(new MyGridViewAdapter(this, mStrs, mThumbIds, i, icon_dimension));  
          gv.setGravity(Gravity.CENTER);  
          gv.setClickable(true);  
          gv.setFocusable(true);  
          gv.setNumColumns(NUMOFCOLUMN);  
          gv.setStretchMode(GridView.STRETCH_COLUMN_WIDTH);
          gv.setHorizontalSpacing(gridview_horizontal_padding);
          gv.setVerticalSpacing(gridview_vertical_padding);
          mLists.add(gv);  
      }         
      page_number.setText("1/"+PageCount);
  }  
  </code></pre>
  
  <h2>GridViewAdapter class:</h2>
  
  <pre><code>public class MyGridViewAdapter extends BaseAdapter 
  {   
      private Animation zoom     = null;
      // view
      int icon_width, icon_height;
      Typeface tf;
      private Context mContext;  
      private List&lt;String&gt; mLists;  
      public static final int PAGE_SIZE = 9; // showing how many items in 1 page  
  
      public MyGridViewAdapter(Context pContext, String[] pStrs, Integer[] image_ref, int page, int width) 
      {  
          this.mContext = pContext;  
          mLists = new ArrayList&lt;String&gt;();  
          int i = page * PAGE_SIZE;  
          int end = i + PAGE_SIZE;  
          while ((i &lt; image_ref.length) &amp;&amp; (i &lt; end)) 
          {  
              mLists.add(pStrs[i]); 
              i++;  
          }  
  
          icon_width = width;
          icon_height = icon_width;
  
          tf = Typeface.createFromAsset(mContext.getAssets(),"fonts/HomegirlKiddo.ttf");   
      }  
  
      @Override  
      public int getCount() 
      {           
          return mLists.size();  
      }  
      @Override  
      public Object getItem(int position) 
      {           
          return mLists.get(position);  
      }  
      @Override  
      public long getItemId(int position) 
      {     
          return position;  
      }  
  
      @Override  
      public View getView(int position, View convertView, ViewGroup parent) 
      {
          ViewHolder holder;
          if (convertView == null)
          {
              LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
              convertView = inflater.inflate(R.layout.menugrid, parent, false);
              holder = new ViewHolder();
  
              holder.img = (ImageView) convertView.findViewById(R.id.imageicon);
              convertView.setTag(holder);
          } 
          else
          {
              holder = (ViewHolder) convertView.getTag();
          }
  
  
          final int myNum = Integer.parseInt(mLists.get(position)) -1;
          ImageView imageView = holder.img;
  
          try
          {
              imageView.setImageBitmap(decodeSampledBitmapFromResource(mContext.getResources(), mThumbIds[myNum], icon_width, icon_width));   
          }
          catch (OutOfMemoryError e)
          {
              ((Index_Gridview)mContext).custom_toast("System Out-of-Error! Restarting...");
              ((Index_Gridview)mContext).restart_app();  
          }
  
  
          imageView.setOnClickListener(new OnImageClickListener(myNum));       
          return convertView;
      }
  
  private Bitmap decodeSampledBitmapFromResource(Resources res, int resId, int reqWidth, int reqHeight) 
  {
      // First decode with inJustDecodeBounds=true to check dimensions
      final BitmapFactory.Options options = new BitmapFactory.Options();
      options.inPreferredConfig = Bitmap.Config.ARGB_8888;
      options.inJustDecodeBounds = true;
      BitmapFactory.decodeResource(res, resId, options);
  
      // Calculate inSampleSize
      options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
  
      // Decode bitmap with inSampleSize set
      options.inJustDecodeBounds = false;
      return BitmapFactory.decodeResource(res, resId, options);
  }
  
  private int calculateInSampleSize (BitmapFactory.Options options, int reqWidth, int reqHeight) 
  {
      // Raw height and width of image
      final int height = options.outHeight;
      final int width = options.outWidth;
      int inSampleSize = 1;
  
      if (height &gt; reqHeight || width &gt; reqWidth) 
      {   
          // Calculate ratios of height and width to requested height and width
          final int heightRatio = Math.round((float) height / (float) reqHeight);
          final int widthRatio = Math.round((float) width / (float) reqWidth);
  
          // Choose the smallest ratio as inSampleSize value, this will guarantee
          // a final image with both dimensions larger than or equal to the requested height and width.
          inSampleSize = heightRatio &lt; widthRatio ? heightRatio : widthRatio;
      }   
      return inSampleSize;
  } 
  
  class ViewHolder 
  {
      ImageView img;
  }
  
  // References to our images in res &gt; drawable
  public Integer[] mThumbIds =  new Integer[] 
      {
          R.drawable.t_aaa, 
          R.drawable.t_bbb, 
                      ....listing of 156 image references here
      }
  </code></pre>
  
  <h2>Question:</h2>
  
  <p>I have already tried to import the Thumbnails via 'decodeSampledBitmapFromResource'. Original thumbnails are around 75KB each. </p>
  
  <p>It will easily run into OOM. How could the codes be enhanced to alleviate the OOM error? </p>
  
  <p>Thanks!!</p>