media - android scanning all .mp3 files in SD Card -
i'm trying scan .mp3 files in sd card , save name. here fragment of code responsible that. 1.what i'm doing wrong? 1. correct path 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) { (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 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 code snippet
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 read mp3 files , store 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) { (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) { (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 songlist songslist.add(songmap); } }
Comments
Post a Comment