Android development saves data to SD card

Source: Internet
Author: User

Objective:
Use the activity of the Openfileoutput () method to save the file, the file is stored in the mobile phone space, the general phone storage space is not very large, storage of small files is OK, if you want to store large files such as video, it is not feasible. For large files like video, we can store it in SDcard. What is sdcard for? You can think of it as a removable hard drive or USB stick.
To use SDcard in the simulator, you need to first create a SDcard card (not really sdcard, just the image file). Creating a sdcard can be created when eclipse creates the emulator, or it can be created using DOS commands, as follows: In the DOS window, enter the tools directory of the Android SDK installation path and create a 2G sdcard with the following command. File suffix can be arbitrarily taken, recommended use. Img:mksdcard 2048M D:\AndroidTool\sdcard.img Java code

The permissions to add access to SDcard in Androidmanifest.xml are as follows:




To store files to SDcard, the program must first determine whether the phone is equipped with sdcard and can read and write.
Note: Access to SDcard must include access to SDcard in Androidmanifest.xml
if (Environment.getexternalstoragestate (). Equals (environment.media_mounted)) {
File Sdcarddir = Environment.getexternalstoragedirectory ();//Get SDcard directory, 2.2:/mnt/sdcart 2.1 When:/sdcard, So using a static method to get the path is a little better.
File SaveFile = new file (Sdcarddir, "abc.txt");
FileOutputStream OutStream = new FileOutputStream (saveFile);

Outstream.write ("Hello". GetBytes ());

Outstream.close ();

The Environment.getexternalstoragestate () method is used to get the state of the sdcard, and if the phone is loaded with sdcard and can read and write, the method returns a state equal to Environment.media_ Mounted.

The Environment.getexternalstoragedirectory () method is used to get the SDcard directory, and of course to get the SDcard directory, you can also write:

File Sdcarddir = new file ("/sdcard"); Get SDcard Directory

File SaveFile = new file (Sdcarddir, "abc.txt");  
//The above two lines can be synthesized in one sentence: file SaveFile = new file ("/sdcard/abc.txt");  
FileOutputStream outstream = new FileOutputStream (saveFile);  
Outstream.write ("Hello Test". GetBytes ( )); Outstream.close ();  
* Focus: Before saving, you need to determine whether sdcard exists and whether you have writable permissions:  
if (Environment.media_ Mounted.endswith (Environment.getexternalstoragestate ()))  
Environment.media_mounted: SDcard exists and has readable writable permissions

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.