Hej community!
I want to read mp3 files from the storage of the device using File = Environment.getexternaldirectory() or something like this.
I get a null pointer exc. when I try to get Files from there. In my phone I looked up the path of a file in a file explorer. It said storage\emulated\0 same as in my code. So, what is wrong?
Thanks in advance.
Use this method to retrieve a List of files.
public List<File> getMP3Files(String directory) {
List<File> files = new ArrayList<File>();
File folder = new File(directory);
for (File file : folder.listFiles()) {
if (file.isFile()) {
if (file.getName().endsWith(".mp3") || file.getName().endsWith(".MP3")) {
files.add(file);
}
}
}
return files;
}
for example if you have your .mp3 files located in: "/storage/emulated/0", you only need.
String directoryPath=Environment.getExternalStorageDirectory().getPath(); // path /storage/emulated/0/
List<File> list = getMP3Files(directoryPath);
If you have your .mp3 in other location, for example i have my .mp3 files in: "/storage/emulated/0/Music/Overkill"
String mp3Directory = "/Music/Overkill";
String directoryPath=Environment.getExternalStorageDirectory().getPath() + mp3Directory;
List<File> list = getMP3Files(directoryPath);
//print in LogCat the list of .mp3:
for (File file : list) {
Log.i("MP3 File name", file.getName());
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With