title: Listing thumbnails per bucket
body: <p>I have 2 classes. First is meant for searching for all folders, I mean Bucket list name. Seconds class makes thumbnails. But I need to have thumbnails per bucket. So when I push on my folder it should shows me all photos in it (thumbnails). Can somebody help me with it?</p>
  
  <p><strong>First class:</strong></p>
  
  <pre><code>public class ThumbnailAdapter extends BaseAdapter {
  
  // Context required for performing queries
  private final Context mContext;
  
  // Cursor for thumbnails
  private final Cursor cursor;
  private final int imgId;
  private final int imgData;
  private final int count;
  
  public ThumbnailAdapter(Context c) {
      this.mContext = c;
  
      // Get list of all images, sorted by last taken first
      final String[] projection = {
              MediaStore.Images.Media.BUCKET_ID,
              MediaStore.Images.Media.BUCKET_DISPLAY_NAME
      };
       String BUCKET_GROUP_BY =
                  "1) GROUP BY 1,(2";
          String BUCKET_ORDER_BY = "MAX(datetaken) DESC";
      cursor = mContext.getContentResolver().query(
              MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
              projection,
              BUCKET_GROUP_BY,
              null,
              BUCKET_ORDER_BY
  
      );
  
      // Set constants (column indices and image count)
      imgId = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.BUCKET_ID);
      imgData = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.BUCKET_DISPLAY_NAME);
      count = cursor.getCount();
      Log.d("ThumbnailAdapter", count + " images found");
  }
  
  @Override
  public int getCount() {
      return count;
  }
  
  @Override
  public Object getItem(int position) {
      return position;
  }
  
  @Override
  public long getItemId(int position) {
      return position;
  }
  
  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
      LinearLayout ll = new LinearLayout(mContext);
  
        ImageView imageView = new ImageView(mContext);
        TextView mytext = new TextView(mContext);
        cursor.moveToPosition(position);
  
  
        mytext.setText(cursor.getString(cursor.getColumnIndex(Media.BUCKET_ID)));
  
        imageView.setImageResource(R.drawable.your_folder_icon);
        ll.addView(imageView);
        ll.addView(mytext);
  
  
  
    return ll;
  }
  
  /**
   * Get the image path from the given position
   * @param position
   * @return
   */
  public String getImagePath(int position) {
      cursor.moveToPosition(position);
      return cursor.getString(imgData);
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Seconds class:
  public class Th extends BaseAdapter {</p>
  
  <pre><code>// Context required for performing queries
  private final Context mContext;
  
  // Cursor for thumbnails
  private final Cursor cursor;
  private final int imgId;
  private final int imgData;
  private final int count;
  
  public Th(Context c) {
      this.mContext = c;
  
      // Get list of all images, sorted by last taken first
      final String[] projection = {
              MediaStore.Images.Media._ID,
              MediaStore.Images.Media.DATA,
              MediaStore.Images.Media.BUCKET_ID
  
      };
  
      cursor = mContext.getContentResolver().query(
              MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
              projection,
              selection,
              null,
              MediaStore.Images.Media.DATE_TAKEN + " DESC"
      );
      imgId = cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID);
      imgData = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
  
      count = cursor.getCount();
      Log.d("ThumbnailAdapter", count + " images found");
  
  
  }
  @Override
  public int getCount() {
      return count;
  }
  @Override
  public Object getItem(int position) {
      return position;
  }
  @Override
  public long getItemId(int position) {
      return position;
  }
  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
      ImageView imageView;
  
      if (convertView == null) {  // if it's not recycled, initialize some attributes
          imageView = new ImageView(mContext);
          //imageView.setLayoutParams(new GridView.LayoutParams(96, 96));
          //imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
          //imageView.setPadding(8, 8, 8, 8);
      } else {
          imageView = (ImageView) convertView;
      }
      cursor.moveToPosition(position);
      final Bitmap thumbnail = MediaStore.Images.Thumbnails.getThumbnail(
              mContext.getContentResolver(),
              cursor.getInt(imgId), 
              MediaStore.Images.Thumbnails.MICRO_KIND,
              null
      );
      imageView.setImageBitmap(thumbnail);
      Log.d("ThumbnailAdapter", "render: " + cursor.getString(imgData));
  
      return imageView;
  
  
  }
  
  public String getImagePath(int position) {
      cursor.moveToPosition(position);
      return cursor.getString(imgData);
  }
  public void remove(int position) {
      remove(position);
      notifyDataSetChanged();
  
  }
  
  }
  </code></pre>
  
  <p>Main Class:</p>
  
  <pre><code>        final GridView grid = (GridView) findViewById(R.id.gridview);
      final ThumbnailAdapter thumbnails = new ThumbnailAdapter(this);
      final Th thumbnails1 = new Th(this);
      grid.setAdapter(thumbnails);
      grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  
          @Override
          public void onItemClick(AdapterView&lt;?&gt; parent, View v, int position,
                  long id) {
  
              grid.setAdapter(thumbnails1);
  
          }
      });
  }
  </code></pre>