当我运行这个方法时,我立即得到一个堆栈溢出异常,所以显然该方法一直递归调用自己,但我不知道为什么。作为参考的文件结构我与测试它是文件夹的负载,并在这些文件夹是文件,没有其他文件夹来自递归目录遍历的堆栈溢出错误
public void files(File[] f)
{
if(f == null){
return;
}
else
{
for(int i = 0; i < f.length; i++)
{
if(f[i].isFile() && (f[i].getName().contains(".mp3") || f[i].getName().contains(".m4a"))) //iterate through files and check if each file matches the required criteria
{
String fullname = f[i].getName();
Log.v("full name", fullname);
String name = null;
if(fullname.contains(".mp3"))
{
name = fullname.substring(0, fullname.lastIndexOf(".mp3"));
}
else if(fullname.contains(".m4a")) //Removing file extensions of music file so they can be displayed using an appropriate name
{
name = fullname.substring(0, fullname.lastIndexOf(".m4a"));
}
list.add(name);
mp3.add(f[i]);
Log.v("added", name);
}
if(f[i].isDirectory())
{
File inner[] = files[i].listFiles();
files(inner);
}
}
}
}
我会做的第一件事是添加更多的日志记录。接下来我要做的是将递归调用改为接受*一个*文件 - 仅仅因为处理起来更简单。 – 2014-09-05 20:57:50
尝试使用4个文件夹和相当多的文件,并工作 – EatBearsForBreakfast 2014-09-05 21:15:28