listview - List All Mp3 files in android -


i developing music player in android stuck in reading mp3 files. here code read mp3 files. not returing files device(although there files copied using adb). want list using album, artist etc. please in this.

final string media_path = environment.getexternalstoragedirectory()+"";  private arraylist<hashmap<string, string>> songslist = new arraylist<hashmap<string, string>>();  // constructor public songsmanager(){  }  /**  * function read mp3 files sdcard  * , store details in arraylist  * */ public arraylist<hashmap<string, string>> getplaylist(){     file home = new file(media_path);      //if (home.listfiles(new fileextensionfilter()).length > 0) {     if (home.listfiles(new fileextensionfilter())!=null) {          (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 filter files having .mp3 extension  * */ class fileextensionfilter implements filenamefilter {     public boolean accept(file dir, string name) {         return (name.endswith(".mp3") || name.endswith(".mp3"));     } } 

here i've modified getplaylist() method. it.

arraylist<hashmap<string,string>> getplaylist(string rootpath) {             arraylist<hashmap<string,string>> filelist = new arraylist<>();               try {                 file rootfolder = new file(rootpath);                 file[] files = rootfolder.listfiles(); //here npe if directory doesn't contains  file,handle this.                 (file file : files) {                     if (file.isdirectory()) {                         if (getplaylist(file.getabsolutepath()) != null) {                             filelist.addall(getplaylist(file.getabsolutepath()));                         } else {                             break;                         }                     } else if (file.getname().endswith(".mp3")) {                         hashmap<string, string> song = new hashmap<>();                         song.put("file_path", file.getabsolutepath());                         song.put("file_name", file.getname());                         filelist.add(song);                     }                 }                 return filelist;             } catch (exception e) {                 return null;             }         } 

you can song name , song path this:

@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main_activity); arraylist<hashmap<string,string>> songlist=getplaylist("/storage/sdcard1/");         if(songlist!=null){         for(int i=0;i<songlist.size();i++){         string filename=songlist.get(i).get("file_name");         string filepath=songlist.get(i).get("file_path");         //here list of file name , file path present in device         log.e("file details "," name ="+filename +" path = "+filepath);         }         }     } 

note: use "/storage/sdcard1/" reading files sdcard , use environment.getexternalstoragedirectory().getabsolutepath() reading files phone memory

hope you.


Comments

Popular posts from this blog

javascript - Thinglink image not visible until browser resize -

firebird - Error "invalid transaction handle (expecting explicit transaction start)" executing script from Delphi -

mongodb - How to keep track of users making Stripe Payments -