Android data storage and access using SD card

Source: Internet
Author: User

UseOpenfileoutput () of activity ()Method To save the file. The file is stored in the mobile phone space. Generally, the storage space of the mobile phone is not very large. It is okay to store some small files. If you want to store large files such as videos, is not feasible. For large files like videos, we can store them in sdcard. What does sdcard do? You can think of it as a mobile hard disk or a USB flash disk.

To use sdcard in the simulator, You need to first create an sdcard (of course, not a real sdcard, but an image file ). You can create sdcard along with the simulator created in eclipse, or use the doscommand to create it, as shown below:
Enter the tools directory in the android SDK installation path in the DOS window, and enter the following command to create an sdcard with a capacity of 2 GB. The file suffix can be obtained at will. IMG is recommended:
Mksdcard 2048 m d: \ androidtool \ sdcard. img

Access sdcard in the program, you needApply for access to sdcard.
The permission to access sdcard in androidmanifest. XML is as follows:
<! -- Create and delete file permissions in sdcard -->
<Uses-Permission Android: Name = "android. Permission. mount_unmount_filesystems"/>
<! -- Write data to sdcard -->
<Uses-Permission Android: Name = "android. Permission. write_external_storage"/>

To store files in sdcard, the program must first determine whether the mobile phone is equipped with sdcard and can read and write data..
Note: The permission to access sdcard must be added to androidmanifest. xml.
If (environment. getexternalstoragestate (). Equals (environment. media_mounted )){
File sdcarddir = environment. getexternalstoragedirectory (); // get the sdcard directory
File SaveFile = new file (sdcarddir, export hello.txt ");
Fileoutputstream outstream = new fileoutputstream (SaveFile );
Outstream. Write ("test". getbytes ());
Outstream. Close ();
}
The environment. getexternalstoragestate () method is used to obtain the sdcard status. If the mobile phone has an sdcard and can be read and written, the returned state is environment. media_mounted.
The environment. getexternalstoragedirectory () method is used to obtain the sdcard directory. To obtain the sdcard directory, you can also write:
File sdcarddir = new file ("/mnt/sdcard"); // get the sdcard directory
File SaveFile = new file (sdcarddir, "itcast.txt ");
// The code above can be combined into a sentence: file SaveFile = new file ("/mnt/sdcard/hello.txt ");
Fileoutputstream outstream = new fileoutputstream (SaveFile );
Outstream. Write ("test". getbytes ());
Outstream. Close ();

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.