列出每个桶的缩略图

时间:2014-06-11 04:53:30

标签: android image gallery photo mediastore

我有2节课。首先是用于搜索所有文件夹,我的意思是存储列表名称。 Seconds类制作缩略图。但我需要每桶都有缩略图。因此,当我按下我的文件夹时,它应该显示我的所有照片(缩略图)。有人可以帮我吗?

头等舱:

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);
}

}

秒级: 公共类Th扩展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 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();

}

}

主类:

        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<?> parent, View v, int position,
                long id) {

            grid.setAdapter(thumbnails1);

        }
    });
}

0 个答案:

没有答案