如何在两个日期之间查询contentresolver?

时间:2014-12-29 09:32:27

标签: android android-contentresolver

如何在两个日期之间查询内容解析器(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");

1 个答案:

答案 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");