简体   繁体   English

如何按降序打印媒体文件

[英]How to Print Media Files in DESCENDING Order

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

Instead of using only DESC , you will have to describe according to which attribute you want to sort data.而不是只使用DESC ,您必须根据要对数据进行排序的属性进行描述。 So instead use this:所以改为使用这个:

Cursor cursor = contentResolver.query(uri, null, null, null, MediaStore.Video.Media.DATE_ADDED + " DESC");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM