【问题标题】:Getting the recent images from gallery from cursor using ContentResolver使用 ContentResolver 从光标中获取最近的图像
【发布时间】:2017-04-11 17:06:06
【问题描述】:

我正在使用Cursor 从图库中获取图像并从图库中获取所有图像,有没有办法只获取少数最近的图像,例如最近拍摄的 20 张图像。

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

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

【问题讨论】:

标签: android cursor image-gallery android-contentresolver


【解决方案1】:

根据文档,query() 的 order by 参数被格式化为 SQL order by 语句。如果您希望值按降序排列而不是默认升序排列,请执行以下操作:

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

但您特别询问使用日期作为排序,所以您可能想要这个:

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

【讨论】:

  • 谢谢,你能提供一个解决方案,从光标处只获取 20 张最近捕获的图像,因为我的主要问题是只获取少量图像,当我在图库中有很多图像时,应用程序会变慢有些人,我想只请求几张图片可以防止延迟
  • 我尝试使用它,但如果我要使用它,我必须删除 orderBy 并且我的应用程序崩溃了才能使用它。任何更好的解决方案将不胜感激。
【解决方案2】:

除了 Doug 的回答,如果你只想要最近的 20 个 orderBy,你可以这样做:

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

【讨论】:

    猜你喜欢
    • 2013-11-23
    • 2021-05-31
    • 1970-01-01
    • 2012-01-23
    • 2021-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-02
    相关资源
    最近更新 更多