I'm creating an android application that plays the users music. I've got it to work fine on an emulator but it's not working when I install it on a phone, it crashes at this line:
int songIndex = new Random().nextInt(songsList.size());
Because songList.size() is returning 0, since it seems the music can't be found when it runs on a phone. I've put a Micro SD card in the phone, and have loaded music onto it (in the root folder). I'm using the following to get the path:
final String MEDIA_PATH = Environment.getExternalStorageDirectory().getAbsolutePath();
On both the emulator and the phone, the returned string from this is /mnt/sdcard. But it's only working on the emulator. I've also included the following permission in my manifest file:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
Any ideas?
EDIT:
I didn't include this because I didn't think it would help much, but this is the code I'm using to actually get the songList:
public ArrayList<HashMap<String, String>> getPlaylist(){
File home = new File(MEDIA_PATH);
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());
//Add song to song list
songsList.add(song);
}
}
return songsList;
}