21. Android data storage from scratch-SD card

Source: Internet
Author: User

 

In the previous section, we learned how to use file for Android data storage. However, such data is stored in applications. Therefore, the size of such stored files must be limited, sometimes we need to store larger files, such as movies, which uses our sdsard memory card. Android also provides sdcard related operations. Environment class can implement this function.

Environmet class

Common Constants

String media_mounted

The external memory of Android is readable and writable.

String media_mounted_read_only

Current Android external memory read-only

 

Common Methods

Method Name

Description

Public static file getdatadirectory ()

Obtain the directory of the data folder in Android

Public static file getdownloadcachedirectory ()

Obtain the directory of Android download/cache content

Public static file getexternalstoragedirectory ()

Obtain the directory of Android external memory, that is, sdcard.

Public static string getexternalstoragestate ()

Obtain the current status of Android external memory

Public static file getrootdirectory ()

Obtain the root folder directory in Android

 

To read sdcard data, follow these steps:

1. First, determine whether the mobile phone device has the sdcard permission and read and write the sdcard.

Environment. getexternalstoragestate (). Equals (environment. media_mounted)

2. Call environment. getexternalstoragedirectory () to obtain the external storage directory.

3. Use Io streams to read and write files in external memory

4. Do not forget to add permissions in androidmainfest. xml.

Note:The following operations must be completed with the following permissions:

<! -- Create and delete file permissions in sdcard --> <uses-Permission Android: Name = "android. Permission. mount_unmount_filesystems"/> <! -- Write data permission to sdcard --> <uses-Permission Android: Name = "android. Permission. write_external_storage"/>

Instance: read and write files on sdcard

Package COM. jiahui. sdcarddemo; import Java. io. file; import Java. io. fileinputstream; import Java. io. filenotfoundexception; import Java. io. fileoutputstream; import Java. io. ioexception; import Java. io. inputstream; import Java. io. outputstream; import Java. io. randomaccessfile; import android. app. activity; import android. OS. bundle; import android. OS. environment; import android. view. view; import android. widget. button; Import android. widget. edittext; import android. widget. textview; public class sdcarddemoactivity extends activity {private button btnwrite, btnread; private edittext edtcontent; private textview tvresult; Private Static final string file_name = "test.txt"; Public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); edtcontent = (edittext) This. findvie Wbyid (R. id. edtcontent); tvresult = (textview) This. findviewbyid (R. id. tvresult); btnread = (button) This. findviewbyid (R. id. btnread); btnwrite = (button) This. findviewbyid (R. id. btnwrite); btnwrite. setonclicklistener (new view. onclicklistener () {@ overridepublic void onclick (view v) {system. out. println ("write data to sdcard"); // first, judge whether sdcardsystem exists. out. println ("environment. getexternalstoragestate (): "+ environment. ge Texternalstoragestate (); system. out. println ("environment. media_mounted: "+ environment. media_mounted); If (environment. getexternalstoragestate (). equals (environment. media_mounted) {string content = edtcontent. gettext (). tostring (); try {// get the sdcard path file sdcarddir = environment. getexternalstoragedirectory (); // sdcard Directory:/mnt/sdcardstring sdcardpath = sdcarddir. getabsolutepath (); system. out. println ("sdca Rddir. getabsolutepath () "+ sdcarddir. getabsolutepath (); file = new file (sdcarddir, file_name); // file = new file (sdcardpath // + file. separator + file_name); // create the randomaccessfile object randomaccessfile RAF = new randomaccessfile (file, "RW") with the specified file; // move the file record pointer to the last Raf. seek (file. length (); // output file content Raf. write (content. getbytes (); RAF. close () ;}catch (exception e) {// todo: handle exception }}}); btnread. Setonclicklistener (new view. onclicklistener () {@ overridepublic void onclick (view v) {If (environment. getexternalstoragestate (). equals (environment. media_mounted) {// obtain the sdcard path stringbuilder sb = new stringbuilder (); try {file sdcarddir = environment. getexternalstoragedirectory (); file = new file (sdcarddir, file_name); inputstream = new fileinputstream (File); int Len = 0; byte [] Buffer = New byte [1024]; while (LEN = inputstream. Read (buffer ))! =-1) {sb. append (new string (buffer, 0, Len);} tvresult. settext (sb. tostring (); // close the stream inputstream. close ();} catch (exception e) {// todo auto-generated catch blocke. printstacktrace ();}}}});}}

 

Effect

File explorer shows the same effect:

Development considerations:

1. You must not forget to add permissions.

<! -- Create and delete file permissions in sdcard --> <uses-Permission Android: Name = "android. Permission. mount_unmount_filesystems"/> <! -- Write data permission to sdcard --> <uses-Permission Android: Name = "android. Permission. write_external_storage"/>

 

2. Check whether the SD card is inserted and whether the SD card can be read and written:

Environment. getexternalstoragestate (). Equals (environment. media_mounted)

 

If you need to reprint reference please indicate the source: http://blog.csdn.net/jiahui524

Source code download: http://download.csdn.net/download/jiahui524/3799819

 

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.