SQLite - How to add an order by to this query that searches from an image bucket
Currently I have a query retrieves images from my bucket in MediaStore and it's in ascending order (the oldest posts appear first). How do I modify my query so that it displays the newest images first?
String[] PROJECTION_BUCKET = {
MediaStore.Images.ImageColumns.BUCKET_ID,
MediaStore.Images.ImageColumns.BUCKET_DISPLAY_NAME,
MediaStore.Images.ImageColumns.DATE_TAKEN,
MediaStore.Images.ImageColumns.DATA};
Uri images = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
//Not sure how to modify query here
Cursor cur = getContentResolver().query(images, PROJECTION_BUCKET,
MediaStore.Images.Media.BUCKET_DISPLAY_NAME + " like ? ",
new String[] {"%Download%"} , null);
Log.i("ListingImages"," query count=" + cur.getCount());
if (cur.moveToFirst()) {
String bucket;
String date;
String data;
int bucketColumn = cur.getColumnIndex(
MediaStore.Images.Media.BUCKET_DISPLAY_NAME);
int dateColumn = cur.getColumnIndex(
MediaStore.Images.Media.DATE_TAKEN);
int dataColumn = cur.getColumnIndex(
MediaStore.Images.Media.DATA);
do {
bucket = cur.getString(bucketColumn);
date = cur.getString(dateColumn);
data = cur.getString(dataColumn);
Log.i("ListingImages", " bucket=" + bucket
+ " date_taken=" + date
+ " _data=" + data);
} while (cur.moveToNext());
}
You will have to use the last argument of the query()
method which is the sortOrder
.
So instead of null
pass the column followed by DESC
to sort descending:
Cursor cur = getContentResolver().query(
images,
PROJECTION_BUCKET,
MediaStore.Images.Media.BUCKET_DISPLAY_NAME + " like ? ",
new String[] {"%Download%"},
MediaStore.Images.Media.DATE_TAKEN + " DESC"
);