As we traverse the folder, we need to add the following permissions because of the associated SD card operation:
<uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission Android:name= "Android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
First, you need to check the SD card mount status:
Boolean sdcard = Environment.getexternalstoragestate (). Equals (environment.media_mounted); if (!sdcard) { toast.maketext (mainactivity.this, "SD card is not mounted", toast.length_short). Show (); MainActivity.this.finish (); }
When the Getexternalstoragestate () Mount state is returned as not mounted, the program prompts the error and ends execution.
The current message of the activity is then obtained through intent, and if it is executed for the first time, then the information intent gets to is empty. The list of SD card root files is now read. If this is not the first time, obtain the last file path information, and then read the file list under this file path.
Intent Intent = Getintent (); Charsequence cs = Intent.getcharsequenceextra ("FilePath"); FilePath for the incoming file path information if (cs! = null) { File File = new file (cs.tostring ()); Tvpath.settext (File.getpath ()); Files = File.listfiles (); } else { File sdfile = Environment.getexternalstoragedirectory (); Tvpath.settext (Sdfile.getpath ()); Files = Sdfile.listfiles (); } RA ("FilePath");
Then, after getting all the file list information, we need to enter it into the ListView, and the ListView data is bound to adapter. The initialization prototype for adapter is:
Simpleadapter (context context, list<? extends Map<string,?>> data, int resource, string[] from, int[] to)//C Ontext is the context, where we take mainactivity.this//data is the source, is a map structure, the final display map value//resource is a resource file, according to this XML file will be in the ListView content Layout// From is the name of the data source, the key value in map//To is the value of the binding ID in the data and resource
Based on this, we instantiate a map to store the data that will eventually need to be displayed, and create a new resource file/res/layout/list_layout.xml to compose the ListView content:
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "android:orientation=" horizontal "android:layout_width=" Match_parent "android:layout_height=" match_parent "android:weightsum=" 1 "> <imageview android:layout_width=" 46DP "android:layout_height=" 45DP " Android:id= "@+id/image"/> <textview android:layout_width= "Wrap_content" android:layout_height= "38DP" android:textappearance= "? Android:attr/textappearancelarge" android:text= "Large text" android:i D= "@+id/filename" android:layout_weight= "0.14"/></LINEARLAYOUT>
list
Finally, instantiate this adapter and bind the ListView to it, adding the item click event for the ListView. If item is a directory, the directory's path is passed through intent to the activity, and then the activity is started. Back at the beginning of the definition intent, this intent will get directory information passed by the activity, and then further access to the file directory based on this directory information.
Simpleadapter adapter = new Simpleadapter (mainactivity.this, List, r.layout.list_layout, new String []{"image", "FileName"}, New Int[]{r.id.image, r.id.filename}); Listview.setadapter (adapter); Listview.setonitemclicklistener (New Adapterview.onitemclicklistener () {@Override public void Onitem Click (adapterview<?> adapterview, view view, int I, long l) {if (Files[i].isdirectory ()) { file[] Childfile = Files[i].listfiles (); if (childfile! = null && childfile.length >= 0) {Intent Intent = new Intent (mainactivit Y.this, Mainactivity.class); Intent.putextra ("FilePath", Files[i].getpath ()); Toast.maketext (Mainactivity.this, Files[i].getpath (), Toast.length_short). Show (); StartActivity (Intent); } } } });
The
Complete code is as follows:
Import Android.content.intent;import Android.os.environment;import android.support.v7.app.AppCompatActivity; Import Android.os.bundle;import android.view.view;import Android.widget.adapterview;import Android.widget.ListView ; Import Android.widget.simpleadapter;import Android.widget.textview;import Android.widget.toast;import Java.io.file;import java.util.arraylist;import Java.util.hashmap;import Java.util.list;public class MainActivity Extends Appcompatactivity {private TextView tvpath; Private ListView ListView; Private file[] files; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Tvpath = (TextView) Findviewbyid (R.id.textview); ListView = (ListView) Findviewbyid (R.id.listview); Boolean sdcard = Environment.getexternalstoragestate (). Equals (environment.media_mounted); if (!sdcard) {Toast.maketext (mainactivity.this, "SD card not mounted", TOAST.length_short). Show (); MainActivity.this.finish (); } Intent Intent = Getintent (); Charsequence cs = Intent.getcharsequenceextra ("FilePath"); if (cs! = null) {File File = new file (cs.tostring ()); Tvpath.settext (File.getpath ()); Files = File.listfiles (); } else {File sdfile = Environment.getexternalstoragedirectory (); Tvpath.settext (Sdfile.getpath ()); Files = Sdfile.listfiles (); } listNotebooks for Android Development (21) Traverse folder