android - How to get the path of an mp3 file from sd card on selecting the filename from playlist? -
i trying make media player in android play song sd card after selecting song name playlist. need path of selected file, want pass activity using intent song played on activity. have made playlist consisting song names , on selecting song getting name of file. unable understand how path of desired file playlist consists files multiple folders. can please help? beginner , still learning android.
here code have written till -
public class playlist extends activity { textview tv; listview list; arrayadapter<string> listadapter ; arraylist<string> listtest; mediaplayer mp; mediacontroller c; string p; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.playlist); tv = (textview)findviewbyid(r.id.tv); list = (listview)findviewbyid(r.id.list); listtest = new arraylist<string>( ); scanner("/sdcard/music/"); if(listtest.size() != 0) { listadapter = new arrayadapter<string> (playlist.this,r.layout.simplerow, listtest); list.setadapter(listadapter); list.setonitemclicklistener(new onitemclicklistener() { public void onitemclick(adapterview<?> parent, view view, int position, long id) { //string p = list.getpath(); string name = (string) ((textview) view).gettext(); toast.maketext(playlist.this,name,toast.length_short).show(); /*intent = new intent(playlist.this,mainactivity.class); bundle b = new bundle(); b.putstring("show", name); i.putextras(b); playlist.this.startactivity(i);*/ } }); } } private void scanner(string path) { // todo auto-generated method stub { try { file fl = new file(path); file[] listoffiles = fl.listfiles(); (file listoffile : listoffiles) { string s = listoffile.getname(); if(s.endswith(".mp3")) { //tv.settext( tv.gettext()+"\n"+s); listtest.add(s); //p = (new file(uri.create(path))).getabsolutepath(); } ///////////////////////////////// file f = new file(path+s+"/"); if (f.exists() && f.isdirectory()) { scanner(path+s+"/"); } ///////////////////////////////// } } catch (exception e) { } } } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.activity_main, menu); return true; } }
here class read mp3 files directory , path
public class test { private list<file> filelist; private class mp3filter implements filenamefilter { public boolean accept(file dir, string name) { return (name.endswith(".mp3")); } } /** * reads file names , store in list * @param mainpath path at, example /sdcard */ public void getmp3files(string mainpath) { file home = new file(mainpath); if (filelist == null) filelist = new arraylist<file>(); mp3filter mp3filter = new mp3filter(); if (home.listfiles(mp3filter) != null && home.listfiles(mp3filter).length > 0) { (file file : home.listfiles(mp3filter)) { //this file path want pass player string filepath = file.getpath(); filelist.add(file); } } } }
Comments
Post a Comment