UseOpenfileoutput () of activity ()Method To save the file. The file is stored in the mobile phone space. Generally, the storage space of the mobile phone is not very large. It is okay to store some small files. If you want to store large files such as videos, is not feasible. For large files like videos, we can store them in sdcard. What does sdcard do? You can think of it as a mobile hard disk or a USB flash disk.
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:
Mksdcard 2048 m d: \ androidtool \ sdcard. img
Access sdcard in the program, you needApply for access to sdcard.
The permission to access sdcard in androidmanifest. XML is as follows:
<! -- Create and delete file permissions in sdcard -->
<Uses-Permission Android: Name = "android. Permission. mount_unmount_filesystems"/>
<! -- Write data to sdcard -->
<Uses-Permission Android: 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
File SaveFile = new file (sdcarddir, export hello.txt ");
Fileoutputstream outstream = new fileoutputstream (SaveFile );
Outstream. Write ("test". 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:
File sdcarddir = new file ("/mnt/sdcard"); // get the sdcard directory
File SaveFile = new file (sdcarddir, "itcast.txt ");
// The code above can be combined into a sentence: file SaveFile = new file ("/mnt/sdcard/hello.txt ");
Fileoutputstream outstream = new fileoutputstream (SaveFile );
Outstream. Write ("test". getbytes ());
Outstream. Close ();