1. Call Environment.getexternalstoragestate () to determine if an SD card is plugged into the phone and the application has permission to read and write the SD card.
Environment.getexternalstoragestate (). Equals (environment.media_mounted)
A return value of TRUE indicates that the application has permission to read and write to the SD card.
2. Call Environment's getExternalStorageDirectory () method to obtain the external memory, which is the directory of the SD card.
3. Use FileInputStream FileOutputStream FileReader FIleWriter to read and write the files in the SD card.
Class Utils {public void Mywrite (String data) throws Exception {File sdfile = Environment.getexternalstoragedirectory (); File F = new file (sdfile, "demo.txt"); FileOutputStream fos = new FileOutputStream (f); Fos.write (Data.getbytes ()); Fos.flush (); Fos.close ();} Public String Myread () throws Exception {File sdfile = Environment.getexternalstoragedirectory (); File F = new file (sdfile, "demo.txt"); FileInputStream fis = new FileInputStream (f); StringBuffer sb = new StringBuffer (), int len = 0;while (len = Fis.read ())! =-1) {sb.append ((char) len);} return sb.tostring ();}} public class Mainactivity extends Activity {private EditText write;private Button Savebutton, Readbutton;private TextView Show; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); show = (TextView) Findviewbyid (r.id.show); write = (EditText) Findviewbyid ( R.id.write) Savebutton = (Button) Findviewbyid (r.id.save); Readbutton = (Button) Findviewbyid (r.id.reAD), final Utils s = new Utils (), try {show.settext (S.myread ()),} catch (Exception E1) {//TODO auto-generated catch Blocke1 . Printstacktrace ();} Savebutton.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {//TODO auto-generated Method Stubtry {S.mywrite (Write.gettext (). toString ())} catch (Exception e) {//TODO auto-generated catch Blocke.printstacktrace ();}}); Readbutton.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {//TODO auto-generated Method Stubtry {Show.settext (S.myread ());} catch (Exception e) {//TODO auto-generated catch Blocke.printstacktrace ();}}} );}}
In order to read and write data on the SD card, you need to add permissions:
Permissions created on Delete:
<uses-permission android:name= "Android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
Permissions written to the SD card:
<uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/>
Android------read SD card content