使用ContentResolver从游标获取图库中的最新图像 [英] Getting the recent images from gallery from cursor using ContentResolver

查看:149
本文介绍了使用ContentResolver从游标获取图库中的最新图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Cursor 从图库中获取图像,并从图库中获取所有图像,有什么办法只能获取很少的最近图像,例如最近20张图像捕获的图像。

I'm using Cursor to get the images from gallery and getting all the images from gallery, is there any way to get only few recent images, such as last 20 image captured.

我面临的另一个问题是图像是从旧到新的顺序,我想以相反的顺序(从新到旧)获得图像。

One more problem, I am facing is the images are coming from older to new order, I want to get the image in reverse order(new to older).

final String[] columns = { MediaStore.Images.Media.DATA,
                MediaStore.Images.Media._ID };
final String orderBy = MediaStore.Images.Media._ID;

我的图像获取代码:

public void getImg(){
    Cursor imagecursor = getContentResolver().query(
            MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null,
            null, orderBy);

    int image_column_index = imagecursor
            .getColumnIndex(MediaStore.Images.Media._ID);
    this.count = imagecursor.getCount();
    this.thumbnails = new Bitmap[this.count];
    this.arrPath = new String[this.count];
    this.thumbnailsselection = new boolean[this.count];
    for (int i = 0; i < this.count; i++) {
        imagecursor.moveToPosition(i);
        int id = imagecursor.getInt(image_column_index);
        int dataColumnIndex = imagecursor
                .getColumnIndex(MediaStore.Images.Media.DATA);
        thumbnails[i] = MediaStore.Images.Thumbnails.getThumbnail(
                getApplicationContext().getContentResolver(), id,
                MediaStore.Images.Thumbnails.MICRO_KIND, null);
        arrPath[i] = imagecursor.getString(dataColumnIndex);
    }
    GridView imagegrid = (GridView) findViewById(R.id.PhoneImageGrid);
    imageAdapter = new ImageAdapter();
    imagegrid.setAdapter(imageAdapter);
    imagecursor.close();
}

我的适配器:

public class ImageAdapter extends BaseAdapter {
    private LayoutInflater mInflater;

    public ImageAdapter() {
        mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public int getCount() {
        return count;
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.galleryitem, null);
            holder.imageview = (ImageView) convertView
                    .findViewById(R.id.thumbImage);
            holder.checkbox = (CheckBox) convertView
                    .findViewById(R.id.itemCheckBox);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.checkbox.setId(position);
        holder.imageview.setId(position);
        holder.checkbox.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                CheckBox cb = (CheckBox) v;
                int id = cb.getId();
                if (thumbnailsselection[id]) {
                    cb.setChecked(false);
                    thumbnailsselection[id] = false;
                } else {
                    cb.setChecked(true);
                    thumbnailsselection[id] = true;
                }
            }
        });

        holder.imageview.setImageBitmap(thumbnails[position]);
        holder.checkbox.setChecked(thumbnailsselection[position]);
        holder.id = position;
        return convertView;
    }
}

class ViewHolder {
    ImageView imageview;
    CheckBox checkbox;
    int id;
}


推荐答案

< a href = http://developer.android.com/reference/android/content/ContentResolver.html#query%28android.net.Uri,%20java.lang.String[],%20java.lang.String,%20java每个文档的.lang.String [],%20java.lang.String%29 rel = noreferrer> query()都按语句的格式设置为SQL顺序。如果希望值以降序而不是默认的升序进行,请执行以下操作:

The order by argument to query(), per documentation, is formatted as a SQL order by statement. If you want the values in descending order as opposed to the default ascending order, do this:

final String orderBy = MediaStore.Images.Media._ID + " DESC";

但是您特别询问使用日期作为排序方式,所以您可能希望这样做:

But you specifically asked about using the date as the ordering, so you probably want this instead:

final String orderBy = MediaStore.Images.Media.DATE_ADDED + " DESC";

这篇关于使用ContentResolver从游标获取图库中的最新图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆