This example for you to share the simplest SD card file traversal procedures for your reference, the specific content as follows
Package Com.wenhao.test.sddemo;
Import Java.io.File;
Import android.app.Activity;
Import Android.os.Bundle;
Import android.os.Environment;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;
Import Android.widget.Toast; The public class Maindemo extends activity {/** called the ' when the ' is the ' The activity ' is the ' the '. */private Button button =
Null
Private File path;
@Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
Button = (button) Findviewbyid (R.id.mybutton); Detect if the SD card exists if (Environment.getexternalstoragestate (). Equals (environment.media_mounted)) {path = EnviR
Onment.getexternalstoragedirectory ();
}else{Toast.maketext (This, "No SD card", Toast.length_long). Show ();
Finish (); Button.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) {
TODO auto-generated Method Stub getallfiles (path);
}
});
//traversal receives a file path and then iterates through and out of all files in the subdirectory of the file private void Getallfiles (file root) {file files[] = Root.listfiles ();
if (Files!= null) {for (File f:files) {if (F.isdirectory ()) {getallfiles (f);
}else{System.out.println (f);
}
}
}
}
}
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.