Android for SDcard expansion card file Operation example detailed _android

Source: Internet
Author: User

Android's operation of SDcard expansion card files is a normal file operation, but there are still places to pay attention to. Like what:

1. Join SDcard Operation Authority ;

2. recognizes the existence of sdcard ;

3. You cannot create files directly in a sdcard root directory, but you need to create a directory first and then create a file ;

Examples are as follows:

(1) Add SDcard operation permission in Androidmanifest.xml

<!--sdcard permissions-->
<uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE" ></ Uses-permission>

(2) Variable declaration:

Private final static String PATH = "/sdcard/digu";
Private final static String FILENAME = "/notes.txt";

(3) write files to SDcard:

/**
* Write file
/
private void Onwrite () {
   try {
     log.d (log_tag, "Start write");
     1. To determine whether there is a sdcard
     if (Environment.MEDIA_MOUNTED.equals (Environment
         . Getexternalstoragestate ())) {
       // Directory
       File Path = new file (path);
       File "
       f = new" (PATH + FILENAME);
       if (!path.exists ()) {
         //2. Create a directory, you can create path.mkdirs () when the application is started
         ();
       if (!f.exists ()) {
         //3. create file
         f.createnewfile ();
       }
       OutputStreamWriter OSW = new OutputStreamWriter (
           new FileOutputStream (f));
       4. Write the file and get the text value from EditView
       osw.write (Editor.gettext (). toString ());
       Osw.close ();
     }
   catch (Exception e) {
     log.d (log_tag, "file Create Error");
   }
 

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.