I have been trying to make a music player application,and as I have just started android, Could any one tell me how can I accces the music files in my sdcard, I have given the location of the sdcard as "\sdcard\" in my String variable MEDIA_PATH ,but what after that? How should the system access the music files put it more simply the files ending with .mp3 format,I don't know which function to use. I'd appreciate your suggestions, thanks.
Asked
Active
Viewed 2,303 times
0
-
1you should do search on internet before posting question ..there are many demo available like [this](http://androidsamples.blogspot.in/2009/06/displaying-list-of-music-files-stored.html) and [this](http://www.helloandroid.com/tutorials/musicdroid-audio-player-part-i) – MKJParekh Mar 17 '12 at 06:38
2 Answers
3
You can get all the mp3 files from rooted sdcard by the following code.
File home = Environment.getExternalStorageDirectory();
if (home.listFiles( new Mp3Filter()).length > 0) {
for (File file : home.listFiles( new Mp3Filter())) {
songs.add(file.getAbsolutePath());
}
ArrayAdapter<String> songList = new ArrayAdapter<String>
(this,R.layout.song_item,songs);
setListAdapter(songList);
}
class Mp3Filter implements FilenameFilter
{
public boolean accept(File dir, String name)
{
return (name.endsWith(".mp3"));
}
}
Here songs is a ArrayList which stores the actual path of the song.

Chirag
- 56,621
- 29
- 151
- 198
-
if you get time please come to here http://chat.stackoverflow.com/rooms/1531/casual-chat – MKJParekh Mar 17 '12 at 06:40
1
Use FileFilter
for scanning the .mp3 files from sdcard. like:
//Check media mounted or not
if(Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()))
{
File sdCardDir=new File("/sdcard");
File sdCardDir=Environment.getExternalStorageDirectory();
ArrayList<File>files=getFiles(file);
private ArrayList<File> getFiles(Filefile){
File files[]=sdCardDir.listFiles(filter);
for(File f:files){
if(f.isDirectory()&&f.canRead()){
getFiles(f);
}else if(f.isFile()){
videlList.add(f);
}
}
//USE FileFilter
FileFilter filter=new FileFilter(){
@Override
public boolean accept(File f){
returnf.isDirectory()||f.getName().matches("^.*?//.(mp3)$");
}
}

ρяσѕρєя K
- 132,198
- 53
- 198
- 213