Android (9) _ Data storage and Access 3_scard introduction

Source: Internet
Author: User

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 with a DOS command, as follows:

    • In the DOS window, enter the tools directory of the Android SDK installation path and enter the following command to create a sdcard with a capacity of 2G, the file suffix can be arbitrarily taken, it is recommended to use. IMG:
      • Mksdcard 2048M D:\AndroidTool\sdcard.img

To access SDcard in the program, you need to request permission to access SDcard.
The permissions to add access to SDcard in Androidmanifest.xml are as follows:
    • <!--Create and delete files in SDcard permissions--
      • <uses-permission android:name= "Android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
    • <!--write data to sdcard permissions--
      • <uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/>

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         file saveFile = new file (Sdcarddir, "itcast.txt"); FileOutputStream OutStream = new FileOutputStream (saveFile); Outstream.write ("Zhao Yazhi Blog". GetBytes ()); Outstream.close ();}


Environment.getexternalstoragestate () Method: Get the state of SDcard

Environment.media_mounted: If the phone is loaded with sdcard and can read and write, the returned status

Environment.getexternalstoragedirectory () Method: Get the SDcard directory, of course, to get the SDcard directory, you can also write:


File Sdcarddir = new file ("/sdcard"); Get SDcard Directory
File SaveFile = new file (Sdcarddir, "itcast.txt");
The above two lines of code can be synthesized one sentence: file SaveFile = new file ("/sdcard/itcast.txt");
FileOutputStream OutStream = new FileOutputStream (saveFile);
Outstream.write ("Zhao Yazhi test". GetBytes ());
Outstream.close ();



Save the file to the SD card and fetch the file from the folder


Remove a file or folder from an SD card


Case 1: Calling the camera and storing the photos on an SD card


Case 2: Instance login, save username and password in SD card


Android (9) _ Data storage and Access 3_scard introduction

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.