问题
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