0

I am Accessing media files using cursor in a array. But i need to print the array i descending order (Meaning Latest Media File in System First)

Cursor cursor = getApplicationContext()
                .getContentResolver()
                .query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, projection, null, null, " DESC");

I Have Tried this but it does not work and application crashes.

public ArrayList < String > getAllMedia() {
    HashSet < String > videoItemHashSet = new HashSet < > ();
    String[] projection = {
        MediaStore.Video.VideoColumns.DATA,
        MediaStore.Video.Media.DISPLAY_NAME
    };
    Cursor cursor = getApplicationContext().getContentResolver().query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, projection, null, null, " DESC");
    try {
        Objects.requireNonNull(cursor).moveToFirst();
        do {
            videoItemHashSet.add((cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA))));
        } while (cursor.moveToNext());

        cursor.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return new ArrayList < > (videoItemHashSet);
}
Ömer Erden
  • 7,680
  • 5
  • 36
  • 45
Abdul Manan
  • 43
  • 1
  • 9

1 Answers1

0

Instead of using only DESC, you will have to describe according to which attribute you want to sort data. So instead use this:

Cursor cursor = contentResolver.query(uri, null, null, null, MediaStore.Video.Media.DATE_ADDED + " DESC");
Sanjay SS
  • 568
  • 4
  • 14