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");
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");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With