Recently, I wrote a program reading Android sdcard and used the environment class static method to get the sdcard path. At the beginning, I did not read the SDK successfully, and the system prompts that the permission is abnormal. Obviously, I am already in androidmanifest. <uses-Permission Android: Name = "android. permission. write_external_storage "/> permission. This exception was reported. After carefully searching for information, I found that I still had a permission <uses-Permission Android: Name =" android. permission. mount_unmount_filesystems "/>. With this permission, you can correctly read the content of the sdcard.
The following is my androidmanifest. xml file.
<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <manifest xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> package = "com. yzq. sdcarlist "Android: versioncode =" 1 "<br/> Android: versionname =" 1.0 "> </P> <p> <application Android: icon = "@ drawable/icon" Android: Label = "@ string/app_name"> <br/> <activity Android: Name = "sdcardlistactivity" Android: theme = "@ Android: style/theme. notitlebar "> <br/> <intent-filter> <br/> <action Android: Name =" android. intent. action. main "/> <br/> <category Android: Name =" android. intent. category. launcher "/> <br/> </intent-filter> <br/> </activity> <br/> </Application> </P> <p> <uses- permission Android: name = "android. permission. write_external_storage "/> <br/> <uses-Permission Android: Name =" android. permission. mount_unmount_filesystems "/> <br/> </manifest>
This is the Java source code.
Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. main); <br/> sdcard_file_list = (listview) findviewbyid (R. id. sdcard_file_list); <br/> simpleadapter adapter = new simpleadapter (this, Data, <br/> android. r. layout. simple_list_item_2, new string [] {"FILENAME", <br/> "filepath"}, new int [] {android. r. id. text1, <br/> android. r. id. text2 }); <Br/> sdcard_file_list.setadapter (adapter); <br/> sdcard_file_list <br/>. setonscrolllistener (New abslistview. onscrolllistener () {</P> <p> @ override <br/> Public void onscrollstatechanged (abslistview view, <br/> int scrollstate) {<br/> If (scrollstate = scroll_state_idle) {</P> <p >}< br/>}</P> <p> @ override <br/> Public void onscroll (abslistview view, <br/> int firstvisibleitem, int visibleitemcount, <br /> Int totalitemcount) {</P> <p >}< br/>}); <br/> readfile (); <br/>}</P> <p> private void readfile () {<br/> file = environment. getexternalstoragedirectory (); <br/> file [] files = file. listfiles (); <br/> readfile (files); <br/>}</P> <p> private void readfile (file [] files) {<br/> If (files! = NULL & files. length> 0) {<br/> for (INT I = 0; I <files. length; I ++) {<br/> If (files [I]. isdirectory () {<br/> readfile (files [I]. listfiles (); <br/>}else {<br/> parsefile (files [I]); <br/>}< br/> if (I = files. length-1) {<br/>}</P> <p> private void parsefile (File file) {<br/> string filename = file. getname (); <br/> string filepath = file. getpath (); <br/> Map <string, string> map = new hashmap <string, string> (); <br/> map. put ("FILENAME", filename); <br/> map. put ("filepath", filepath); <br/> data. add (MAP); <br/>}
I used the static method of the Environment class to get the path of the sdcard, and then read all the files in the directory recursively.