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