Android 使用ContentResolver从光标获取库中最近的图像

Android 使用ContentResolver从光标获取库中最近的图像,android,cursor,image-gallery,android-contentresolver,Android,Cursor,Image Gallery,Android Contentresolver,我正在使用光标从gallery中获取图像,并从gallery中获取所有图像,是否有任何方法仅获取最近的几张图像,例如最近拍摄的20张图像 还有一个问题,我面临的是图像从旧到新的顺序,我想以相反的顺序获得图像(从新到旧) 获取图像的我的代码: public void getImg(){ Cursor imagecursor = getContentResolver().query( MediaStore.Images.Media.EXTERNAL_CONTENT_U

我正在使用
光标
从gallery中获取图像,并从gallery中获取所有图像,是否有任何方法仅获取最近的几张图像,例如最近拍摄的20张图像

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

获取图像的我的代码:

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();
}
根据文档,到的order by参数的格式为SQL order by语句。如果希望值按降序而不是默认的升序排列,请执行以下操作:

final String orderBy = MediaStore.Images.Media._ID + " DESC";
但您特别询问了使用日期作为订购日期的问题,因此您可能希望改为:

final String orderBy = MediaStore.Images.Media.DATE_ADDED + " DESC";
根据文档,到的order by参数的格式为SQL order by语句。如果希望值按降序而不是默认的升序排列,请执行以下操作:

final String orderBy = MediaStore.Images.Media._ID + " DESC";
但您特别询问了使用日期作为订购日期的问题,因此您可能希望改为:

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

除了Doug的回答之外,如果您只希望orderBy提供最近的20个,您可以这样做:


final String orderBy=MediaStore.Images.Media.DATE\u ADDED+“DESC LIMIT 20”

除了Doug的答案外,如果您只希望orderBy提供最近的20个,您可以这样做:


final String orderBy=MediaStore.Images.Media.DATE\u ADDED+“DESC LIMIT 20”

谢谢,伙计,你能给出一个解决方案,只从光标上获取20张最近捕获的图像吗,因为我的主要问题是只获取很少的图像,当我的多媒体资料中有很多图像时,应用程序会慢一些,我想只请求五张图像会阻止我尝试使用它,但是,如果我要使用它,我必须删除orderBy,我的应用程序正在崩溃,如果要使用它,任何更好的解决方案都将受到赞赏。感谢你,你能给出一个解决方案,只从光标上获取20张最近捕获的图像,因为我的主要问题是只获取很少的图像,当我的图库中有很多图像时,应用程序有时会变慢,我想只请求五张图片会阻止我尝试使用它,但是如果我使用它,我必须删除orderBy,我的应用程序正在崩溃,我会感谢任何更好的解决方案。谢谢,我会检查这个:)谢谢,我会检查这个:)