4

我想让用户从手机的图库中选择一些图像,然后将它们加载到应用程序中。对于 Intent.ACTION_PICK 和 startActivityForResult(intent, SELECT_PHOTO),这似乎是一项简单的任务。

但是,我需要按日期范围过滤图库中的结果。我需要设置一个初始日期和一个结束日期,并且只有在这两个日期之间拍摄的照片应该显示(或可选择)。有谁知道如何实现这一目标?无论如何,我似乎无法过滤图库结果。

谢谢!

4

3 回答 3

5

我也遇到了这个任务,经过几个小时的尝试终于解决了:)。您需要使用媒体存储并查询它从日期和日期传递的时间,它返回一个位图数组下面是对我有用的代码。

final String[] columns = { MediaStore.Images.Media.DATA,
                MediaStore.Images.Media._ID,
                MediaStore.Images.Media.DATE_TAKEN,
                MediaStore.Images.Media.DATE_ADDED };
final String orderBy = MediaStore.Images.Media._ID;
Cursor imagecursor = managedQuery(
            MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns,
            MediaStore.Images.Media.DATE_TAKEN + ">? and "
                + MediaStore.Images.Media.DATE_TAKEN + "<?",
                new String[] { "" + from, "" + to }, orderBy + " DESC");
int image_column_index = imagecursor.getColumnIndex(MediaStore.Images.Media._ID);
this.count = imagecursor.getCount();
Log.w("", "count is :" + count);
this.thumbnails = new Bitmap[this.count];
for (int i = 0; i < this.count; i++) {
            imagecursor.moveToPosition(i);
            int id = imagecursor.getInt(image_column_index);
            thumbnails[i] = MediaStore.Images.Thumbnails.getThumbnail(ctx
                    .getApplicationContext().getContentResolver(), id,
                    MediaStore.Images.Thumbnails.MICRO_KIND, null);
     }

imagecursor.close();
于 2014-01-03T12:34:14.297 回答
0

查询以在日期之间获取图像:

private Cursor cursorVar;

Calendar fromDateVar = Calendar.getInstance();
fromDateVar.set(2015, 5, 30);

Calendar toDateVar = Calendar.getInstance();
toDateVar.set(2016, 5, 30);

cursorVar = getContentResolver().query(
        MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null,
        MediaStore.Images.Media.DATE_TAKEN + ">? and "
                + MediaStore.Images.Media.DATE_TAKEN + "<?",
        new String[] {fromDateVar.getTimeInMillis() + "", toDateVar.getTimeInMillis() + ""},
        MediaStore.Images.Media.DATE_TAKEN + " DESC");
于 2017-07-24T20:48:41.117 回答
0
Calendar fromDateVar = Calendar.getInstance();
fromDateVar.set(2020, 01, 30);

Calendar toDateVar = Calendar.getInstance();
toDateVar.set(2020, 05, 30);


cursor = getContentResolver().query(
        MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection,
        MediaStore.Images.Media.DATE_TAKEN + ">? and "
                + MediaStore.Images.Media.DATE_TAKEN + "<?",
        new String[]{fromDateVar.getTimeInMillis() + "", toDateVar.getTimeInMillis() + ""},
        MediaStore.Images.Media.DATE_TAKEN + " DESC");


column_index_data = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
column_index_folder_name = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.BUCKET_DISPLAY_NAME);
while (cursor.moveToNext()) {
    absolutePathOfImage = cursor.getString(column_index_data);
    listOfAllImages.add(absolutePathOfImage);

}
于 2020-06-11T05:42:23.153 回答