Android------read SD card content

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.