SoFunction
Updated on 2025-03-11

Android obtains the directory file list in order of time

This example shares the specific code for Android to obtain the file list in chronological order for your reference. The specific content is as follows

/**
   * Get all files in the directory (sorted by time)
   *
   * @param path
   * @return
   */
 public static List<File> listFileSortByModifyTime(String path) {
  List<File> list = getFiles(path, new ArrayList<File>());
  if (list != null && () > 0) {
   (list, new Comparator<File>() {
    public int compare(File file, File newFile) {
     if (() < ()) {
      return -1;
     } else if (() == ()) {
      return 0;
     } else {
      return 1;
     }
    }
   });
  }
  return list;
 }

 /**
   *
   * Get all files in the directory
   *
   * @param realpath
   * @param files
   * @return
   */
 public static List<File> getFiles(String realpath, List<File> files) {
  File realFile = new File(realpath);
  if (()) {
   File[] subfiles = ();
   for (File file : subfiles) {
    if (()) {
     getFiles((), files);
    } else {
     (file);
    }
   }
  }
  return files;
 }

test:

List<File> list = listFileSortByModifyTime("/storage/sdcard1/DCIM/Camera/");
    int i = 0;
    for (File file : list) {
     i++;
     ("ZMS",
       i + ":" + () + " = "
         + ());
    }

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.