Finally. APP modification is mainly used to test how to use code to operate the SD card on the Android platform.
Main. xml
<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: Orientation = "vertical" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent" <br/> <textview <br/> Android: id = "@ + ID/textview" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "wrap_content" <br/> Android: TEXT = "@ string/Hello" <br/> </linearlayout> <br/>
Sdcardactivity. Java
Package cn.com. android. sdcard; </P> <p> Import Java. io. file; <br/> Import Java. io. ioexception; </P> <p> Import android. app. activity; <br/> Import android. OS. bundle; <br/> Import android. OS. environment; <br/> Import android. util. log; <br/> Import android. widget. textview; <br/>/** <br/> * @ author chenzheng_java <br/> * @ since 2011/03/02 <br/> * @ description tests Android sdcard-based applications. <br /> * <br/> */<br/> Public C Lass sdcardactivity extends activity {<br/> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. main); <br/> string result = ""; <br/> // obtain the absolute path of the default file storage location <br/> file filedir = This. getfilesdir (); <br/> log. I ("file path", filedir. getpath (); <br/> result + = "file path/n filedir. getpath (): "+ filedir. getpath (); <br/> result + = "/n Filedir. getabsolutepath (): "+ filedir. getabsolutepath (); <br/> result + = "/n filedir. getname (): "+ filedir. getname (); </P> <p> // obtain the current status of the external storage device <br/> string externalstoragestate = environment. getexternalstoragestate (); <br/> // environment. media_mounted indicates that an external storage device exists and can be read and written. <br/> If (externalstoragestate. equals (environment. media_mounted) {<br/> // getexternalstoragedirectory obtains the path of the external storage device. <br/> log. I ("sdcard path ", Environment. getexternalstoragedirectory (). getabsolutepath (); <br/> file = new file (environment. getexternalstoragedirectory (), "chenzheng_java.txt"); <br/> result + = "sdcard absolute path:" + environment. getexternalstoragedirectory (). getabsolutepath (); <br/> result + = "/n sdcard path:" + environment. getexternalstoragedirectory (). getpath (); <br/> If (! File. exists () {<br/> try {<br/> file. createnewfile (); <br/> log. I ("result", "created successfully. "); <Br/>}catch (ioexception e) {<br/> log. I (" result "," creation failed. "); <Br/> E. printstacktrace (); <br/>}</P> <p> textview = (textview) findviewbyid (R. id. textview); <br/> textview. settext (result); </P> <p >}< br/>}
The androidmanifest. xml file code is as follows:
<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <manifest xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> package = "cn.com. android. sdcard "<br/> Android: versioncode =" 1 "<br/> Android: versionname =" 1.0 "> <br/> <uses-SDK Android: minsdkversion = "8"/> <br/> <! -- Add write permission for sdcard --> <br/> <uses-Permission Android: Name = "android. permission. write_external_storage "> </uses-Permission> </P> <p> <application Android: icon =" @ drawable/icon "Android: label = "@ string/app_name"> <br/> <activity Android: Name = ". sdcardactivity "<br/> Android: Label =" @ string/app_name "> <br/> <intent-filter> <br/> <action Android: Name =" android. intent. action. main "/> <br/> <category Android: Name =" android. intent. category. launcher "/> <br/> </intent-filter> <br/> </activity> </P> <p> </Application> <br/> </manifest>
--------------------------------------------------------------------------
We can see from the code that if we want to operate the SD card,
First, we must first have an SD card. The SD here is simulated by an image file.
Second, we want to use manifest in Android. the permission declaration in the XML file tells the system that our application has the right to access the SD card. <uses-Permission Android: Name = "android. permission. write_external_storage "> </uses-Permission>.
Step 3, we can manage the SD card by operating the file code.
---------------------------------------------------------------------------
Appendix:
Write Permission to the SD card. <uses-Permission Android: Name = "android. Permission. write_external_storage"> </uses-Permission>
Allows you to install and uninstall mobile devices.
, <Uses-Permission Android: Name = "android. Permission. mount_unmount_filesystems
"> </Uses-Permission>