如何在两个日期之间查询内容解析器(MediaStore.Images.Media.EXTERNAL_CONTENT_URI
)?我正在尝试使用代码,但这不起作用。
Calendar c = Calendar.getInstance()
Date date2 = c.getTime();
c.add(Calendar.YEAR,-1);
Date date1 = c.getTime();
Cursor cursor = getContentResolver().query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
projection,
MediaStore.MediaColumns.DATE_ADDED + ">=? and "+MediaStore.MediaColumns.DATE_ADDED +"<=?",
new String[]{"" + date1,""+date2},
MediaStore.Images.ImageColumns.DATE_TAKEN + " DESC");
答案 0 :(得分:8)
从这个MediaColumns.DATE_ADDED日期时间长格式。
为此,您需要传递long
数据而不是Date
对象
更改您的查询
Cursor cursor = getContentResolver().query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
projection,
MediaStore.MediaColumns.DATE_ADDED + ">=? and "+MediaStore.MediaColumns.DATE_ADDED +"<=?",
new String[]{"" + date1,""+date2},
MediaStore.Images.ImageColumns.DATE_TAKEN + " DESC");
到
Cursor cursor = getContentResolver().query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
projection,
MediaStore.MediaColumns.DATE_ADDED + ">=? and "+MediaStore.MediaColumns.DATE_ADDED +"<=?",
new String[]{"" + date1.getTtime()/1000,""+date2.getTime()/1000},
MediaStore.Images.ImageColumns.DATE_TAKEN + " DESC");