我有代码读取每个文件夹并尝试在移动设备中读取mp3文件。但问题是,它会占用设备中的每个子文件夹以及它的拥抱时间。我想要的是。我想限制我的搜索条件直到5子文件夹。平均文件夹和3个子文件夹。像
/存储/ sdcard0 /
/存储/ sdcard0 /安卓/
/存储/ sdcard0 / Android设备/数据/
在此文件夹之后需要检查下一个文件夹。即 /存储/ sdcard0 /下载
这是我的代码。
int folderno = 0;
public ArrayList<HashMap<String, String>> getPlayList(String rootPath) {
ArrayList<HashMap<String, String>> fileList = new ArrayList<>();
try {
File rootFolder = new File(rootPath);
File[] files = rootFolder.listFiles(); //here you will get NPE if directory doesn't contains any file,handle it like this.
for (File file : files) {
if (folderno <3 && file.isDirectory()) {
folderno = folderno + 1;
if (getPlayList(file.getAbsolutePath()) != null) {
fileList.addAll(getPlayList(file.getAbsolutePath()));
} else {
folderno = 0;
//break;
}
} else if (file.getName().endsWith(".mp3")) {
// folderno = folderno - 1;
HashMap<String, String> song = new HashMap<>();
song.put("file_path", file.getAbsolutePath());
song.put("file_name", file.getName());
fileList.add(song);
}
}
folderno = 0;
return fileList;
} catch (Exception e) {
return null;
}
}
我试图包含一些逻辑,但现在正在运作。我创建了folderno作为变量并检查它。 提前谢谢....
答案 0 :(得分:0)
我在eclipse中试过这个
private int count = 0;
private void readFiveDir(File file) {
count++;
System.out.println("Directory/File Name : " + file.getName());
System.out.println("Folder No : " + count);
if(file.isDirectory()) {
if(count == 5) {
return;
}
for(File currFile : file.listFiles()) {
readFiveDir(currFile);
}
}
}
我的目录结构:
val runOnGrid : Boolean = args.configMap.get("runOnGrid") match {
case Some(value) => value // <=== Changed
case _ => false
}
试试并告诉我。