【发布时间】:2018-06-11 14:12:29
【问题描述】:
我想按字母顺序处理 Android 文件。我在 PC 上用 Java 测试过,它按字母顺序对文件进行排序。然而,当我在 Android 中测试时,我发现 Android 是按大小读取文件的。
您能帮我找到一种按字母顺序读取 Android 文件的方法吗?
安卓代码:
String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM) + File.separator + "testface" + File.separator;
Log.d("MyTag", path);
File folder = new File(path);
File[] listOfFiles = folder.listFiles();
Arrays.sort(listOfFiles);
Log.d("MyTag", "no.of files: " + listOfFiles.length);
long startTime2 = System.nanoTime();
for (int i = 0; i < listOfFiles.length; i++) {
if (listOfFiles[i].isFile()) {
String filepath = path + listOfFiles[i].getName();
Log.d("MyTag", "appface_image_filepath: " + filepath);
}
}
调试日志:
01-01 22:09:11.042 8484-8484/com.example.nasif.facedetectionloop D/MyTag: /storage/emulated/0/DCIM/testface/
01-01 22:09:11.043 8484-8484/com.example.nasif.facedetectionloop D/MyTag: no.of files: 10
01-01 22:09:11.044 8484-8484/com.example.nasif.facedetectionloop D/MyTag: appface_image_filepath: /storage/emulated/0/DCIM/testface/11.jpg
01-01 22:09:11.044 8484-8484/com.example.nasif.facedetectionloop D/MyTag: appface_image_filepath: /storage/emulated/0/DCIM/testface/12.jpg
01-01 22:09:11.044 8484-8484/com.example.nasif.facedetectionloop D/MyTag: appface_image_filepath: /storage/emulated/0/DCIM/testface/17.jpg
01-01 22:09:11.044 8484-8484/com.example.nasif.facedetectionloop D/MyTag: appface_image_filepath: /storage/emulated/0/DCIM/testface/18.jpg
01-01 22:09:11.044 8484-8484/com.example.nasif.facedetectionloop D/MyTag: appface_image_filepath: /storage/emulated/0/DCIM/testface/19.jpg
01-01 22:09:11.045 8484-8484/com.example.nasif.facedetectionloop D/MyTag: appface_image_filepath: /storage/emulated/0/DCIM/testface/20.jpg
01-01 22:09:11.045 8484-8484/com.example.nasif.facedetectionloop D/MyTag: appface_image_filepath: /storage/emulated/0/DCIM/testface/27.jpg
01-01 22:09:11.045 8484-8484/com.example.nasif.facedetectionloop D/MyTag: appface_image_filepath: /storage/emulated/0/DCIM/testface/28.jpg
01-01 22:09:11.046 8484-8484/com.example.nasif.facedetectionloop D/MyTag: appface_image_filepath: /storage/emulated/0/DCIM/testface/3.jpg
01-01 22:09:11.046 8484-8484/com.example.nasif.facedetectionloop D/MyTag: appface_image_filepath: /storage/emulated/0/DCIM/testface/4.jpg
【问题讨论】:
-
其实按字母顺序,28排在3之前。仅此而已。答案请查看here