Android development saves data to the SD card, androidsd
Preface:
Use the openFileOutput () method of Activity to save files. The files are stored in the mobile phone space. Generally, the mobile phone storage space is not very large, and it is okay to store some small files, it is not feasible to store large files such as videos. 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 you can use the doscommand to create it. In the Dos window, enter the tools directory of the android SDK installation path, run the following command to create an SDCard with a capacity of 2 GB. The file suffix can be obtained at will. img: mksdcard 2048 m d: \ AndroidTool \ sdcard. img Java code
. Add the permission to access SDCard in AndroidManifest. xml as follows:
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. The value of 2.2 is/mnt/sdcart 2.1 and/sdcard. Therefore, it is better to use the static method to obtain the path.
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 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 ("/sdcard"); // get the SDCard directory
File saveFile = new File (sdCardDir, "abc.txt ");
// The code above can be combined into one sentence: File saveFile = new File ("/sdcard/abc.txt ");
FileOutputStream outStream = new FileOutputStream (saveFile );
OutStream. write ("Hello test". getBytes (); 26. outStream. close ();
* Important: Before saving the SDK, You need to determine whether the SDCard exists and whether it has the write permission:
If (Environment. MEDIA_MOUNTED.endsWith (Environment. getExternalStorageState ()))
Environment. MEDIA_MOUNTED: An SDCard exists and has read and write permissions.
How does android add and save data to the SD card?
Add a permission in manifest
<Uses-permission android: name = "android. permission. WRITE_EXTERNAL_STORAGE"/>
This is the same as java's method for operating files on pc systems. You can use
Environment. getExternalStorageDirectory (). getAbsolutePath.
In android programming, how does one save a small amount of data to a specified file on the SD card without passing through the database?
First, you must declare the permission.
<Uses-permission android: name = "android. permission. WRITE_EXTERNAL_STORAGE">
</Uses-permission>
The others are similar to normal read/write files.
For example, get an object.
Final String FILE_NAME = "abc.txt ";
File file = new File (Environment. getExternalStorageDirectory () + "/" + FILE_NAME );
Then open the stream, write the content, and close the stream, which is the same as reading and writing files in JAVA.