I am working on a mp3 player app, which plays .mp3 files present anywhere inside an internal SD card.
I have used the following codes to fetch the .mp3 files present in
Trying using this:
File root = Environment.getExternalStorageDirectory();
That will get you the root of the external storage, provided that there is one. Then you can filter out files that aren't .mp3
Also consider looking at this: List all of one file type on Android device?
To get a reference to a file located on: sdcard/music/song.mp3:
File file = Environment.getExternalStorageDirectory()+"/music/song.mp3";
import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.HashMap;
enter code here
import android.os.Environment;
public class SongsManager {
final String MEDIA_PATH = Environment.getExternalStorageDirectory()``
.getPath() + "/";
private ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
private String mp3Pattern = ".mp3";
// Constructor
public SongsManager(){
}
/**
* Function to read all mp3 files from sdcard
* and store the details in ArrayList
* */
public ArrayList<HashMap<String, String>> getPlayList(){
System.out.println(MEDIA_PATH);
if (MEDIA_PATH != null) {
File home = new File(MEDIA_PATH);
File[] listFiles = home.listFiles();
if (listFiles != null && listFiles.length > 0) {
for (File file : listFiles) {
System.out.println(file.getAbsolutePath());
if (file.isDirectory()) {
scanDirectory(file);
} else {
addSongToList(file);
}
}
}
}
// return songs list array
return songsList;
}
private void scanDirectory(File directory) {
if (directory != null) {
File[] listFiles = directory.listFiles();
if (listFiles != null && listFiles.length > 0) {
for (File file : listFiles) {
if (file.isDirectory()) {
scanDirectory(file);
} else {
addSongToList(file);
}
}
}
}
}
private void addSongToList(File song) {
if (song.getName().endsWith(mp3Pattern)) {
HashMap<String, String> songMap = new HashMap<String, String>();
songMap.put("songTitle",
song.getName().substring(0, (song.getName().length() - 4)));
songMap.put("songPath", song.getPath());
// Adding each song to SongList
songsList.add(songMap);
}
}
/**
* Class to filter files which are having .mp3 extension
* */
class FileExtensionFilter implements FilenameFilter {
public boolean accept(File dir, String name) {
return (name.endsWith(".mp3") || name.endsWith(".MP3"));
}
}
}
You can get the root directory of the external sdcard using this code of line
File root = Environment.getExternalStorageDirectory();
Now you can do the same thing as you do for internal
ArrayList<File> inFiles = new ArrayList<File>();
File list[] = root.listFiles(); // here use the root object of File class to the list of files and directory from the external storage
//Log.i("DIR", "PATH" +file.getPath());
for (int i = 0; i < list.length; i++)
{
// myList.add( list[i].getName() );
File temp_file = new File(file.getAbsolutePath(),list[i].getName());
//Log.i("DIR", "PATH" +temp_file.getAbsolutePath());
if (temp_file.listFiles() != null)
{
//Log.i("inside", "call fn");
listfiles(temp_file);
}
else
{
if (list[i].getName().toLowerCase().contains(".mp3"))
{
inFiles.add(list[i]);
//Log.e("Music", list[i].getName());
}
}
}
A slight Modification to Tushar Gupta's Codes. This worked fine for me. I have complete code for a Music Player
import android.os.Environment;
import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.HashMap;
public class SongsManager {
final String MEDIA_PATH = Environment.getExternalStorageDirectory().getPath() + "/";
private ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
private String mp3Pattern = ".mp3";
// Constructor
public SongsManager(){
}
/**
* Function to read all mp3 files from sdcard
* and store the details in ArrayList
* */
public ArrayList<HashMap<String, String>> getPlayList(){
System.out.println(MEDIA_PATH);
if (MEDIA_PATH != null) {
File home = new File(MEDIA_PATH);
File[] listFiles = home.listFiles();
if (listFiles != null && listFiles.length > 0) {
for (File file : listFiles) {
System.out.println(file.getAbsolutePath());
if (file.isDirectory()) {
scanDirectory(file);
} else {
addSongToList(file);
}
}
}
}
// return songs list array
return songsList;
}
private void scanDirectory(File directory) {
if (directory != null) {
File[] listFiles = directory.listFiles();
if (listFiles != null && listFiles.length > 0) {
for (File file : listFiles) {
if (file.isDirectory()) {
scanDirectory(file);
} else {
addSongToList(file);
}
}
}
}
}
private void addSongToList(File song) {
if (song.getName().endsWith(mp3Pattern)) {
HashMap<String, String> songMap = new HashMap<String, String>();
songMap.put("songTitle",
song.getName().substring(0, (song.getName().length() - 4)));
songMap.put("songPath", song.getPath());
// Adding each song to SongList
songsList.add(songMap);
}
}
/**
* Class to filter files which are having .mp3 extension
* */
class FileExtensionFilter implements FilenameFilter {
public boolean accept(File dir, String name) {
return (name.endsWith(".mp3") || name.endsWith(".MP3"));
}
}
}