how to list images on sd card sorted according to time

谁说我不能喝 提交于 2019-12-25 13:17:22

问题


I can get the list of images in sd card using fololowing`

but these are not sorted

how can I get them sorted either by name or time

thanks in advance

String ExternalStorageDirectoryPath = Environment
                    .getExternalStorageDirectory().getAbsolutePath();

            String targetPath = ExternalStorageDirectoryPath + "/somedirectoryname/";

            File targetDirectory = new File(targetPath);

            File[] files = targetDirectory.listFiles();`
for (File file : files1) {
                    list.add(file.getAbsolutePath().toString());
                }

回答1:


Maybe it will be easier if you use the built-in content provider

     MediaStore.Images.Media.query(cr, uri, projection, where, orderBy)

something like this:

    String[] projection = {MediaColumns._ID, MediaColumns.DISPLAY_NAME, MediaColumns.DATE_ADDED};
    Cursor c = MediaStore.Images.Media.query(getContentResolver(), MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, null, MediaColumns.DATE_ADDED);

then you can loop the cursor to do whatever you need.




回答2:


- Use lastModified() method of File to get the time the file was modified.

- Store them in Collection like ArrayList<File>.

- Use java.util.Comparator<T> to compare and sort them according to time and name.



来源:https://stackoverflow.com/questions/13824233/how-to-list-images-on-sd-card-sorted-according-to-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!