Store files in sdcard

Source: Internet
Author: User

Use the openfileoutput () method of activity to save files. The files are stored in the mobile phone space. Generally, the mobile phone storage space is not very large, and it is okay to store some small files, it is not feasible to store large files such as videos. For large files like videos, we can store them in sdcard.

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:

Mksdcard2048md: \ androidtool \ sdcard. img

To access sdcard in a program, you need to apply for the permission to access sdcard.

The permission to access sdcard in androidmanifest. XML is as follows:

<! -- Create and delete file permissions in sdcard -->

<Uses-permissionandroid: Name = "android. Permission. mount_unmount_filesystems"/>

<! -- Write data to sdcard -->

<Uses-permissionandroid: 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

Filesavefile = new file(sdcarddir,“test.txt ");

Fileoutputstreamoutstream = new fileoutputstream (SaveFile );

Outstream. Write ("test text". 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:

Filesdcarddir = new file ("/sdcard"); // get the sdcard directory

Filesavefile = new file (sdcarddir, "test.txt ");

// The code above can be combined into a sentence: file SaveFile = new file ("/sdcard/test.txt ");

Fileoutputstream outstream = new fileoutputstream (SaveFile );

Outstream. Write ("test text". 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.