WEB开发网
开发学院手机开发Android 开发 Android遍历特定目录下所有文件 阅读

Android遍历特定目录下所有文件

 2012-10-25 16:04:29 来源:WEB开发网   
核心提示: private boolean deleteLastFromFloder(String path) {boolean success = false;try {ArrayList<File> images = new ArrayList<File>();getFiles(images,

  private boolean deleteLastFromFloder(String path) {
boolean success = false;
try {
ArrayList<File> images = new ArrayList<File>();
getFiles(images, path);
File latestSavedImage = images.get(0);
if (latestSavedImage.exists()) {
for (int i = 1; i < images.size(); i++) {
File nextFile = images.get(i);
if (nextFile.lastModified() > latestSavedImage.lastModified()) {
latestSavedImage = nextFile;
}
}

Log.e("brady", "images = " + latestSavedImage.getAbsolutePath());
success = latestSavedImage.delete();
}
} catch (Exception e) {
e.printStackTrace();
}
return success;
}

private void getFiles(ArrayList<File> fileList, String path) {
File[] allFiles = new File(path).listFiles();
for (int i = 0; i < allFiles.length; i++) {
File file = allFiles[i];
if (file.isFile()) {
fileList.add(file);
} else if (!file.getAbsolutePath().contains(".thumnail")) {
getFiles(fileList, file.getAbsolutePath());
}
}
}

Tags:Android 遍历 特定

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接