Adroid learning-SD card operations (1)

Source: Internet
Author: User

As Android learns, when we open the input and output streams of files through openfileinput or openfileoutput of context, all files in the data folder of the application are opened by the program lock, the size of the files stored in this way may be limited-after all, the built-in storage space of the mobile phone is limited.

In order to better access the application's big file data, the application needs to read and write files on the SD card. The storage capability of mobile phones is greatly expanded through the SD card. So let's first learn how to read and write files on the SD card.

 To read and write files on an SD card, follow these steps:

(1) Call the getexternalstoragestate () method of environment to determine whether the SD card is installed on the mobile phone or simulator, and the application has the permission to read and write the SD card.

(2) Call the getexternalstoragedirectory () method of environment to obtain the external memory, that is, the SD card directory.

(3) Use fileinputstrean, fileoutputstrean, filereader, and filewriter to read and write files in the SD card.

Two considerations:

A. Are you sure you have an SD card on your phone or simulator. (If no new one is created, refer to another LZ article)

B. To read and write data on the SD card, you must add the read and write permissions to the androidmanifest. xml file in the program list.

 

When LZ is written, only two text boxes are used on the page. Two buttons. The original code is not attached here.

The following is the essence of reading and writing files on the SD card. Hope everyone can help.

The main user sets a listening event for the two buttons, and calls the read () and write () Methods written by himself in the listening event to implement the SD card read and write operations.

The read () and write () methods are the key to reading and writing SD card files. You can retain them for future development.

 

Key Methods:

Environment. getexternalstoragedirectory () after confirming that the application has the permission to read and write the SD card, call this method to obtain the storage directory corresponding to the SD card.

 

(If you do not like the environment method to obtain the path of the SD card file, you can use the relative path to represent the path of the SD card, then, the program will know whether the mobile phone or simulator has an SD card by checking whether the half-segment path exists)

 

LZ hopes that everyone can communicate and make progress together. I hope this chapter will be useful to you.

 

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.