Android Write File to SD card

Source: Internet
Author: User

1. Main. xml

<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: layout_width = "match_parent" Android: layout_height = "match_parent" Android: Orientation = "vertical"> <edittext Android: id = "@ + ID/numet" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: hint = "Enter the file name"/> <edittext Android: id = "@ + ID/contentet" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: hint = "Please enter the file content" // prompt text Android: inputtype = "textmultiline" Android: minlines = "3" type = "regxph" text = "yourobjectname"/> // the minimum value is three rows. The value increases automatically when the value is greater than the value: layout_height = "match_parent" Android: Orientation = "horizontal"> <button Android: Id = "@ + ID/sdcbt" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: text = "Save to SD card" Android: onclick = "onclick"/> <button Android: Id = "@ + ID/rombt" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: text = "Save to mobile phone" Android: onclick = "onclick"/> </linearlayout>

2. mainactivity

Package COM. njupt. file1; import android. OS. bundle; import android. OS. environment; import android. app. activity; import android. view. menu; import android. view. view; import android. widget. edittext; import android. widget. toast; public class mainactivity extends activity {private edittext numet; private edittext contentet; @ overrideprotected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); numet = (edittext) findviewbyid (R. id. numet); contentet = (edittext) findviewbyid (R. id. contentet) ;}@ overrideprotected void onresume () {super. onresume (); // environment. getexternalstoragestate (). obtain the status of the SD card and determine whether to install or remove findviewbyid (R. id. sdcbt ). setenabled (environment. getexternalstoragestate (). equals (environment. media_mounted);} @ overridepublic Boolean oncreateoptionsmenu (menu) {// inflate the menu; this adds items to the action bar if it is present. getmenuinflater (). inflate (R. menu. main, menu); Return true;} public void onclick (view v) {try {string name = numet. gettext (). tostring (); string content = contentet. gettext (). tostring (); fileservice service = new fileservice (); Switch (v. GETID () {case R. id. sdcbt: service. savetosdcard (name, content); break; case R. id. rombt: service. savetorom (name, content); break;} toast. maketext (getapplicationcontext (), "saved successfully", toast. length_short ). show ();} catch (exception e) {e. printstacktrace (); toast. maketext (getapplication (), "dacard exception", toast. length_short ). show ();}}}

3. fileservice

Package COM. njupt. file1; import Java. io. file; import Java. io. fileoutputstream; import Java. io. ioexception; import android. OS. environment; public class fileservice {public void savetosdcard (string name, string content) {fileoutputstream Fos = NULL; try {// environment. getexternalstoragedirectory (). Obtain the path file = new file (environment. getexternalstoragedirectory (), name); Fos = new fileoutputstream (File); FOS. write (content. getbytes ();} catch (exception e) {e. printstacktrace ();} finally {try {FOS. close ();} catch (ioexception e) {e. printstacktrace () ;}} public void savetorom (string name, string content) {// todo auto-generated method stub }}

4. androidmanifest. xml

Finally, it is matched with androidmanifest. xml.

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

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.