I'm trying to scan all .mp3 files in my SD card and save its name. here is a fragment of code which is responsible for that. 1.What i'm doing wrong? 1. Which is correct path for SD Card /mnt/sdcard or /sdcard ?
public class PlayerActivity extends Activity
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.player_activity);
}
public class SongsManager
{
// SDCard Path
//final String MEDIA_PATH = new String(MediaStore.Audio.Media.getContentUri("external").toString());
private ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
public SongsManager()
{
}
public ArrayList<HashMap<String, String>> getPlayList()
{
// File home = new File(MEDIA_PATH);
File home = Environment.getExternalStorageDirectory();
if (home.listFiles(new FileExtensionFilter()).length > 0)
{
for (File file : home.listFiles(new FileExtensionFilter()))
{
HashMap<String, String> song = new HashMap<String, String>();
song.put("songTitle", file.getName().substring(0, (file.getName().length() - 4)));
song.put("songPath", file.getPath());
// Adding each song to SongList
songsList.add(song);
}
}
// return songs list array
return songsList;
}
class FileExtensionFilter implements FilenameFilter
{
public boolean accept(File dir, String name)
{
return (name.endsWith(".mp3") || name.endsWith(".MP3"));
}
}
}
}
Thanks in advance.
try this code snippet
The
Environment.getExternalStorageDirectory().getPath()
is usually of the form "/storage/emulated/0/" or so.You split this to get the root -
This works!
I'm not sure what's wrong in your code. But you can give this approach a try:
To scan entire whole sdcard for mp3 call
scanDirectory(Environment.getExternalStorageDirectory());
The problem is with what android consider as external storage. It is not always the sd card that you add to your device. the internal memory can also be external storage. /sdcard or /mnt/sdcard is the same location and it may point to your internal storage. so you have to scan the /mnt folder completely. like in some device /mnt/sdcard will be internal memory and /mnt/extSdCard will be the one you have added. File home = Environment.getExternalStorageDirectory(); this method will return the first external storage and not all.
try this: final String MEDIA_PATH = "/storage/";