----Main.java
public class main extends activity {private textview textview;private button button;private listview listview;public file currentparentfile;public file[] currentfiles;public static string sdcarddir ;static {try {// The path of the SD card Sdcarddir = environment.getexternalstoragedirectory (). Getcanonicalpath ();} catch (ioexception e) {e.printstacktrace ();}} @Overrideprotected void oncreate (bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (R.layout.main);textview = (TextView) findviewbyid ( R.ID.TEXTVIEW1);button = (Button) findviewbyid (r.id.button1);listview = (ListView) findviewbyid (R.ID.LISTVIEW1); File root = new file (Sdcarddir); if (Root.exists ()) {currentparentfile = root; Currentfiles = root.listfiles (); Updatelistview (currentfiles);} ListView.setonitemclicklistener (New onitemclicklistener () {@Overridepublic void onitemclick ( Adapterview<?> parent, view view,int position, long id) {if ( Currentfiles[position].isfile ()) return; File[] tmp = currentfiles[position].listfiles ();if (tmp == null | | tmp.length == 0) {toast.maketext (main.this, "The current path is invalid, or there is no file", Toast.length_short). Show ( );} else {currentParentFile = currentFiles[position];currentFiles = tmp; Updatelistview (Currentfiles);}}); Button.setonclicklistener (New onclicklistener () {@Overridepublic void onclick (View v) {try {if (!currentparentfile.getcanonicalpath (). Equals (Sdcarddir)) {currentparentfile = currentparentfile.getparentfile (); Currentfiles = currentparentfile.listfiles (); Updatelistview (currentfiles);} Elsereturn;} catch (ioexception e) &NBsp {// todo auto-generated catch blocke.printstacktrace ();}});} Private void updatelistview (file[] files) {List<Map<String, Object>> itemps = new ArrayList<Map<String, Object>> (;for ) (int i = 0; i < files.length; i++) {map<string, object> listitem = new HashMap<String, Object> ();if (Files[i].isdirectory ()) listitem.put ("icon" , r.drawable.folder); Elselistitem.put ("icon", r.drawable.file); Listitem.put ("name", files[i]. GetName ()); Itemps.add (ListItem);} Simpleadapter simpleadapter = new simpleadapter (this, itemps,r.layout.listitem, new string[] { "icon", "name" }, new int[] {R.id.imageView1, r.id.text }); Listview.setadapter (Simpleadapter); Try {textview.settext ("Current path is:" + Currentparentfile.getcanonicalpath ());} catch (ioexception e) {e.printstacktrace ();}}
Layout file----main.xml
<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android" xmlns:tools= "Http://schemas.android.com/tools" android:layout_width= "Match_parent" android:layout_height= "Match_parent" tools:context= "${ Relativepackage}.${activityclass} " > <TextView android:id= "@+id/textview1" android:layout_ Width= "Wrap_content" android:layout_height= "Wrap_content" android:text= "Current path:" /> <button android:id= "@+id/button1" android:layout_width= "Wrap_content" android:layout_ height= "Wrap_content" &Nbsp; android:layout_alignparentbottom= "true" android:layout_centerhorizontal= "true" android:text = "Back to top level" /> <ListView Android:id= "@+id/listview1" android:layout_width= "Match_parent" android:layout_height= "Wrap_content" android:layout_above= "@+id/button1" Android:layout_alignparentleft= "true" android:layout_below= "@+ Id/textview1 " > </ListView></RelativeLayout>
Effect:
The layout of each bar of the ListView:
---listitem.xml
<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= "http// Schemas.android.com/apk/res/android " android:layout_width=" Match_parent " android:layout_height= "Match_parent" > <ImageView android:id= "@+id/imageview1" android:layout_width= "Wrap_content" android:layout_height = "Wrap_content" android:layout_alignparentleft= "true" android:layout_alignparenttop= "true" android:src= "@drawable/ic_launcher" /> <TextView android:id= "@+id/text" android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:layout_alignparenttop= "true" android:layout_margintop= "15DP" android:layout_torightof= "@+id/ ImageView1 " android:text=" TextView " /></ Relativelayout>
Demo Run Effect:
Simple implementation browse files on Android SD card