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

Anu*_*nuj 3 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");
Run Code Online (Sandbox Code Playgroud)

kal*_*pvs 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");
Run Code Online (Sandbox Code Playgroud)

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");
Run Code Online (Sandbox Code Playgroud)

  • 谢谢,现在工作正常.问题是date1.getTime()以毫秒为单位给出时间,但DATE_ADDED以秒为单位存储,所以我需要除以1000来纠正它. (2认同)