ユーザーにディレクトリだけを選択してもらいたいので、mp3 ファイルを自分で見つける必要があります。
「.mp3」で終わるファイルを探して、ディレクトリを再帰的にトラバースできます。
public static void findMp3s(File root, List<File> toBuildUp) {
// if the File is not a directory, and the name ends with mp3
// we will add it to our list of mp3s
if (!root.isDirectory() && root.getName().endsWith("mp3")) {
toBuildUp.add(root);
return;
}
if (!file.isDirectory())
return;
// Now, we know that root is a Directory
// We will look through every file and directory under root,
// and recursively look for more mp3 files
for (File f: root.listFiles()){
findMp3s(f, toBuildUp);
}
}
上記のメソッドは、すべてのディレクトリを再帰的に走査しtoBuildUp
、そのディレクトリの下にあるすべての mp3 ファイルを取り込みます。
このメソッドを次のように呼び出します。
List<File> allMp3s = new ArrayList<File>();
findAllMp3s(selectedDirectory, allMp3s);