How to save data to SDCard

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.