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