1.写一个类实现FileFilter接口
public class MP3FileFilter implements FileFilter {
@Override
public boolean accept(File file) {
if(file.isDirectory())
return true;
else
{
String name = file.getName();
if(name.endsWith(.mp3) || name.endsWith(.mp4))
return true;
else
return false;
}
}
}
2.传一个路径,获取改路径下的所有mp3 and mp4文件
/**
* get all the music file in the rootpath.
* @param rootPath
*/
public void getAllFilePath(String rootPath)
{
File file = new File(rootPath);
File[] files = file.listFiles(new MP3FileFilter());
for(int i=0;i<files.length;i++)
{
if(files[i].isDirectory())
{
getAllFilePath(files[i].getPath());
}
else
{
mArrayListMusicPaths.add(files[i].getPath());
mArrayListMusicNames.add(files[i].getName());
System.out.println(files[i].getPath());
}
}
}
原文链接:http://blog.csdn.net/xiangyong2008/article/details/5899740