Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to query contentresolver between two dates?

How to query content resolver (MediaStore.Images.Media.EXTERNAL_CONTENT_URI) between two dates? I am trying following code but this is not working.

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");
like image 679
Anuj Avatar asked Oct 20 '25 04:10

Anuj


1 Answers

From this MediaColumns.DATE_ADDED date time in long format.

for this you need to pass long data instead of Date object

Change your Query

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

into

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");
like image 139
kalyan pvs Avatar answered Oct 22 '25 17:10

kalyan pvs



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!