In this article, we will discuss how to scan all mp3 files and not only from a specific path for an android media player. We will use a recursive method for scanning files. Scan all folders and sort out all mp3 files using the File and FileFilter class.
Implementation
Java
import java.io.File; import java.io.FileFilter; // Java program for scan all mp3 files in given directory public class Main { public static final String C_DIRECTORY = "C:\\" ; public static void main(String[] args) { scanAllFile(C_DIRECTORY); } // recursive function for scan all files in directory public static void scanAllFile(String path) { File file = new File(path); File[] files = file.listFiles(); if (files == null ) return ; for (File f : files) { if (f.isDirectory() && f.exists()) { try { scanAllFile(f.getPath()); } catch (Exception e) { e.printStackTrace(); continue ; } } else if (!f.isDirectory() && f.exists()) { // using file filter if (filter.accept(f)) { System.out.println(f.getName()); } } } } // file filter for sort mp3 files static FileFilter filter = new FileFilter() { @Override public boolean accept(File file) { if (file.getName().endsWith( ".mp3" ) || file.getName().endsWith( ".mp3" )) { return true ; } return false ; } }; } |
Output:
break.mp3 error.mp3 foldedAreas.mp3 quickFixes.mp3 taskEnded.mp3 terminalBell.mp3 warning.mp3 house_lo.mp3