Android action files on SD card

Source: Internet
Author: User

(1) Description: The operation of the file on the SD card needs to add the following permissions

Create and delete file permissions on an SD card

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

Permission to write data on an SD card
<uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/>

The file on the SD card operation and my previous article almost, if there is not understand can see my previous article

Android Read-write file Example

http://blog.csdn.net/liuzuyi200/article/details/25049183

Just added a way to determine if the SD card exists

Environment.getexternalstoragestate (). Equals (environment.media_mounted)

(2) Layout file

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layout_width=" Fill_parent "android:layout_height=" fill_parent "android:orientation=" vertical "&G    T <relativelayout android:layout_width= "fill_parent" android:layout_height= "Wrap_content" Android:or ientation= "vertical" > <textview android:id= "@+id/label_01" android:layout_width= "Wrap_ Content "android:layout_height=" wrap_content "android:text=" @string/name "Android:textsiz            E= "20DP"/> <edittext android:id= "@+id/filename" android:layout_width= "160DP" android:layout_height= "Wrap_content" android:layout_aligntop= "@id/label_01" android:layout_toright of= "@id/label_01"/> </RelativeLayout> <textview android:id= "@+id/label_02" android:layout        _width= "Wrap_content"android:layout_height= "Wrap_content" android:text= "@string/neirong"/> <edittext android:id= "@+id/c        Ontent "android:layout_width=" fill_parent "android:layout_height=" 120px "android:ems=" > <requestfocus/> </EditText> <linearlayout android:layout_width= "Fill_parent" Android: layout_height= "wrap_content" android:orientation= "horizontal" > <button android:id= "@+id/sa Vebutton "android:layout_width=" wrap_content "android:layout_height=" Wrap_content "Androi            D:gravity= "Right" android:text= "@string/save"/> <button android:id= "@+id/readbutton" Android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:gravity= " Right "android:text=" @string/read "/> </LinearLayout> <textview android:id=" @+id/textc Ontent "Android:lAyout_width= "Match_parent" android:layout_height= "Wrap_content" android:layout_weight= "0.10"/></Linea Rlayout>


(3) Code

Package Com.liuzuyi.file;import Java.io.bytearrayoutputstream;import Java.io.file;import java.io.FileInputStream; Import Java.io.fileoutputstream;import Java.io.inputstream;import Java.io.outputstream;import Android.support.v7.app.actionbaractivity;import Android.support.v7.app.actionbar;import Android.support.v4.app.fragment;import Android.app.activity;import Android.content.context;import Android.os.bundle;import Android.os.environment;import Android.util.log;import Android.view.LayoutInflater;import Android.view.menu;import Android.view.menuitem;import Android.view.view;import Android.view.View.OnClickListener; Import Android.view.viewgroup;import Android.widget.button;import Android.widget.edittext;import Android.widget.textview;import Android.widget.toast;import Android.os.build;public class MainActivity extends Activity {private EditText filename;private EditText context;private TextView textcontent;private static final String TA G= "Simplefile";p rotected void OnCreate (Bundle savedInstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Filename= (EditText) This.findviewbyid (R.id.filename); context= (EditText) This.findviewbyid (r.id.content); textcontent= (TextView) This.findviewbyid (r.id.textcontent); Button savebtn= (button) This.findviewbyid (R.id.savebutton); Button viewbtn= (button) This.findviewbyid (R.id.readbutton); Savebtn.setonclicklistener (L);   Viewbtn.setonclicklistener (l);} Private View.onclicklistener L =new Onclicklistener () {public void OnClick (View v) {button button = (button) v;//filter out the opening end of the empty Lattice String namestr = Filename.gettext (). toString (). Trim ();  String contentstr =context.gettext (). toString (); Switch (Button.getid ()) {case R.id.savebutton:string resid_s = "Success"; OutputStream Fileos = null;try {if (environment. Getexternalstoragestate (). Equals (environment.media_mounted)) {FileOutputStream OutStream = new FileOutputStream ("/  sdcard/"+namestr+". txt ") Outstream.write (Contentstr.getbytes ()); Outstream.close (); }elsereturn;} Catch (Exception e) {resid_s = "faile"; E.printstacktrace ();} Toast.maketext (Mainactivity.this, Resid_s,toast.length_long). Show (); LOG.I (TAG, NAMESTR); LOG.I (TAG, contentstr); break;case r.id.readbutton:string resid_v = "Success"; InputStream filsis= null; String contentst = null;try {if (Environment.getexternalstoragestate (). Equals (environment.media_mounted)) {File Sdcarddir=environment.getexternalstoragedirectory (); File SaveFile = new file (sdcarddir,namestr+ ". txt"); FileInputStream instream = new FileInputStream (saveFile);  Bytearrayoutputstream Ostream = new Bytearrayoutputstream (); byte[] buffer = new Byte[1024];int len =-1;  while (len = instream.read (buffer))! =-1) {ostream.write (Buffer,0,len);  } CONTENTST = Ostream.tostring ();  Ostream.close ();   Instream.close (); }else{toast.maketext (Mainactivity.this, "SD card not Present", Toast.length_long). Show ();}} catch (Exception e) {resid_v = "faile"; E.printstacktrace ();} Textcontent.settext (CONTENTST); LOG.I (TAG, CONTENTST); Toast.maketext (MainactivitY.this, Resid_v,toast.length_long). Show ();  LOG.I (TAG,NAMESTR); break;} }    };}


Related Article

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.