Development Environment:
Win XP + eclipse-jee-helios (version 3.6) + ADT (version 10.0.1) + Android SDK (version 10 );
Test environment for simulators and real machines: Android2.2
The data is stored in the storage and reading of the Android learning notes file. It directly saves the data to the storage space provided by the mobile phone. These files are generally relatively small files. How can we store large data in SDCard?
Use the File project created in the storage and reading of the Android learning notes File to save the data to the SDCard.
Directory structure of the File project
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/09533142Z-0.png "title =" 1.png"/>
In FileService. java of the business class, write a method saveToSDCard () that is saved to the SDCard. The Code is as follows:
// Save the data to SDCard public void saveToSDCard (String filename, String content) throws Exception {// get the file object, find the path/File file = new File ("mnt/SDCard", filename); File file = new File (Environment. getExternalStorageDirectory (), filename); // obtain the file output stream object FileOutputStream outStream = new FileOutputStream (file); outStream. write (content. getBytes (); outStream. close ();}
In the MainActivity. java class, call the Save saveToSDCard () method to save the data to the SDCard. The Code is as follows:
Try {if (Environment. getExternalStorageState (). equals (Environment. MEDIA_MOUNTED) {service. saveToSDCard (filename, content); // Save the data to SDCard Toast. makeText (getApplication (), R. string. success, 1 ). show (); // saved successfully} else {Toast. makeText (getApplication (), R. string. sdcarderror, 1 ). show (); // failed to save} catch (Exception e) {Toast. makeText (getApplication (), R. string. fail, 1 ). show (); e. printStackTrace ();}
In the configuration file AndroidManifest. xml, declare the permissions to create files and write data to SDCard:
<! -- Create and delete objects in SDCard --> <uses-permission android: name = "android. permission. MOUNT_UNMOUNT_FILESYSTEMS"/> <! -- Write data to SDCard --> <uses-permission android: name = "android. permission. WRITE_EXTERNAL_STORAGE"/>
Deploy the application to the simulation. When you click Save, the saved file is created under/mnt/sdcard.
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/0953316062-1.png "title =" 2.png"/>
This article from the "Flowers bloom" blog, please be sure to keep this source http://020618.blog.51cto.com/6098149/1295127