Android Entry Development SD card read and write operation

Source: Internet
Author: User

The reading and writing of SD cards is the most common operation we have in the process of developing Android applications. The following describes how the SD card reads and writes:

1. Get the root directory of the SD card

[Java]View Plaincopy
    1. String sdcardroot = Environment.getexternalstoragedirectory (). GetAbsolutePath ();



2. Create a folder directory on the SD card

[Java]View Plaincopy
    1. /**
    2. * Create a directory on the SD card
    3. */
    4. Public File Createdironsdcard (String dir)
    5. {
    6. File Dirfile = new File (sdcardroot + file.separator + dir +file.separator);
    7. LOG.V ("Createdironsdcard", Sdcardroot + file.separator + dir +file.separator);
    8. Dirfile.mkdirs ();
    9. return dirfile;
    10. }


3. Create a file on the SD card

[Java]View Plaincopy
  1. /**
  2. * Create a file on the SD card
  3. */
  4. Public File Createfileonsdcard (string fileName, String dir) throws IOException
  5. {
  6. File File = new file (sdcardroot + file.separator + dir + file.separator + fileName);
  7. LOG.V ("Createfileonsdcard", Sdcardroot + file.separator + dir + file.separator + fileName);
  8. File.createnewfile ();
  9. return file;
  10. }


4. Determine if the file exists in a directory on the SD card

[Java]View Plaincopy < param name= "allowfullscreen" value= "false" >< param name= "wmode" value= "Transparent" >
    1. /**
    2. * Determine if the file exists on the SD card
    3. */
    4. Public Boolean isfileexist (String fileName, string path)
    5. {
    6. File File = new file (sdcardroot + path + file.separator + fileName);
    7. return file.exists ();
    8. }

5. Write the data to the SD card specified directory file

[Java]View Plaincopy
  1. <span style="White-space:pre" > </span>/**
  2. * Write data to SD card
  3. */
  4. Public File Writedata2sdcard (string path, string fileName, InputStream data)
  5. {
  6. File file = null;
  7. OutputStream output = null;
  8. try {
  9. Createdironsdcard (path); //Create a directory
  10. File = Createfileonsdcard (fileName, path); //Create file
  11. Output = new FileOutputStream (file);
  12. byte buffer[] = new byte[1024]; //write 2K data each time
  13. int temp;
  14. While (temp = data.read (buffer))! =-1)
  15. {
  16. Output.write (buffer,0,temp);
  17. }
  18. Output.flush ();
  19. } catch (Exception e) {
  20. E.printstacktrace ();
  21. }
  22. finally{
  23. try {
  24. Output.close (); //Turn off Data flow operations
  25. } catch (Exception E2) {
  26. E2.printstacktrace ();
  27. }
  28. }
  29. return file;
  30. }

One more important thing:

to operate the SD card, you must apply for permission:

<uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/>

Transferred from: http://blog.csdn.net/newjerryj/article/details/8829179

Android Entry Development SD card read and write operation (GO)

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.