Android project on the inevitable use of SD card storage, the previous period of time after the use, now think up a summary, by the way convenient to review. Also write a small demo.
Source Address: http://download.csdn.net/detail/qq_16064871/8548857
The following code is posted, with comments on the code.
First, Datamanage class
Package Com.example.storagemanagedemo;import Java.io.file;import Java.io.fileinputstream;import Java.io.fileoutputstream;import Java.io.ioexception;import Android.os.environment;import Android.util.Log;public Class Datamanage {//Open TXT file public string opentxtfile (string strpathfilename) {string strtxtdata = "";//get SD card storage path root directory file Pathfile = Environment.getexternalstoragedirectory ();//Determine if the SD card has a Boolean sdcardexist = Environment.getexternalstoragestate (). Equals (Android.os.Environment.MEDIA_MOUNTED); if (sdcardexist) {String Strpathfilename = pathfile.tostring () + strpathfilename;//determine if the path exists for this file Absolutpathfilename = new files ( Strpathfilename), if (!absolutpathfilename.exists ()) {} else {strtxtdata = Readtxtdatasdfile (Strpathfilename);}} return strtxtdata;} Save txt Format File public boolean savetxtfile (String strpath, String strfilename,string strsavetxtdata) {//Determine if SD card exists Boolean Sdcardexist = Environment.getexternalstoragestate (). Equals (Android.os.Environment.MEDIA_MOUNTED); if (sdcardexist) {//Get SD card storage path root directory file Pathfile = Environment.getexternalstoragedirectory ();//Create folder according to the path specified by SD card file Absolutpathfile = new File (pathfile.tostring () + strpath), if (!absolutpathfile.exists ()) {absolutpathfile.mkdirs ();} Creates a file in the specified folder string strpathfilename = pathfile.tostring () + strpath+ strFileName; File NameFile = new file (strpathfilename), if (!namefile.exists ()) {try {namefile.createnewfile ();} catch (IOException e) {E.printstacktrace (); LOG.I ("Show", e.tostring ());}} Calling a function to write data to a file try {witetxtdatasdfile (strpathfilename, strsavetxtdata);} catch (IOException e) {//TODO auto-generated Catch Blocke.printstacktrace ();}} return false;} private string Readtxtdatasdfile (String strpathfilename) {string strtxtdata = ""; try {fileinputstream FileInputStream = n EW FileInputStream (strpathfilename); int length = Fileinputstream.available (); byte[] buffer = new Byte[length]; Fileinputstream.read (buffer); strtxtdata = new String (buffer); Fileinputstream.close ();} catch (Exception e) {e.printstacktrace ();} Return Strtxtdata;} Write data to a file private void Witetxtdatasdfile (String strpathfilename, String strsavetxtdata) throws IOException {file WriteFile = new File (strpathfilename); FileOutputStream FileOutputStream = new FileOutputStream (WriteFile); byte[] buffer = strsavetxtdata.getbytes (); try { Fileoutputstream.write (buffer); Fileoutputstream.close ();} catch (IOException e) {e.printstacktrace ();}}}
II, Mainactivity class
Package Com.example.storagemanagedemo;import Java.io.file;import Android.os.bundle;import android.os.Environment; Import Android.view.view;import android.widget.edittext;import Android.widget.textview;import android.app.Activity ;p ublic class Mainactivity extends Activity {private Datamanage mdatamanage = new Datamanage ();p rivate TextView SHOWTEXTVI Ew;private TextView storagetextview;private EditText inputedittext; @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); ShowTextView = ( TextView) Findviewbyid (R.ID.TEXTVIEW3); Storagetextview = (TextView) Findviewbyid (r.id.textview1); Inputedittext = ( EditText) Findviewbyid (R.ID.EDITTEXT1); File pathfile = Environment.getexternalstoragedirectory (); String strpath = "Storage location and file name:" + pathfile.tostring () + "/txt/result.txt"; Storagetextview.settext (strpath);} Press the key to save the file public void Saveprasedata (View v) {mdatamanage.savetxtfile ("/txt", "/result.txt", Inputedittext.gettext (). tOstring ());} Press the key to open the file public void Openprasedata (View v) {String strshowdatastring = ""; strshowdatastring = Mdatamanage.opentxtfile ("/ Txt/result.txt "); Showtextview.settext (strshowdatastring);}}
three, XML
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "Tools:context =". Mainactivity "> <scrollview android:id=" @+id/scrollview_text "android:layout_width=" Match_parent " android:layout_height= "Match_parent" > <linearlayout android:layout_width= "match_parent" android:layout_height= "match_parent" android:orientation= "vertical" > <linearlayout Android:layout_width= "Match_parent" android:layout_height= "Wrap_content" > & Lt TextView android:layout_width= "wrap_content" android:layout_height= "Wrap_content" android:text= "input data:"/> <edittext android:id= "@+id/edittext1" Android:layout_width= "WraP_content "android:layout_height=" Wrap_content "android:layout_weight=" 1 " Android:ems= "Ten" > <requestfocus/> </EditText> </ linearlayout> <linearlayout android:layout_width= "Match_parent" Android:lay out_height= "60DP" > <textview android:id= "@+id/textview2" Andro Id:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:text= "open Data: "/> <textview android:id=" @+id/textview3 "Android:layout_wi Dth= "Match_parent" android:layout_height= "50DP" android:text= "TextView"/> </LinearLayout> <linearlayout android:layout_width= "Match_parent" a Ndroid:layout_height= "WRAp_content "> <textview android:id=" @+id/textview1 "Android:lay Out_width= "Wrap_content" android:layout_height= "wrap_content" android:text= "Storage location and file Name: "/> </LinearLayout> <linearlayout android:layout_width=" Match_parent " android:layout_height= "50DP" android:layout_margintop= "5DP" > <button Android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:layout_marginleft= "5dip" android:onclick= "Saveprasedata" Android:text = "Write sd card"/> <button android:layout_width= "Wrap_content" Android : layout_height= "wrap_content" android:layout_marginleft= "5dip" android:onclick= "Open Prasedata " android:text= "Open File"/> </LinearLayout> </LinearLayout> </scrollview></l Inearlayout>
Four, remember to join in Androidmanifest permissions.
<uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission Android:name= "Android.permission.READ_EXTERNAL_STORAGE"/> <uses-permission android:name= " Android.permission.MOUNT_UNMOUNT_FILESYSTEMS "/>
That's it, you must remember to join the privilege. Beginners continue to grow, welcome to communicate.
Project Resources Download: http://download.csdn.net/detail/qq_16064871/8548857
Reprint Please specify the Source: http://blog.csdn.net/qq_16064871
Android SD Storage Summary