The seventh session describes the internal storage of read-write files click the Open link.
This has a comparison of the problem, assuming that the system memory is not enough, kill the application can not be executed, or the application was uninstalled by the user after reinstallation. Previously saved usernames and passwords are not echoed. Therefore, it is necessary to pay attention to this problem
So save the file to the SD card. The 15th words written today, "read and write files in external storage"
First, the layout file is the same as the seventh sentence:
<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 "Android:paddi ngbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_horizontal_margin" Android: paddingright= "@dimen/activity_horizontal_margin" android:paddingtop= "@dimen/activity_vertical_margin" tools: Context= ". Mainactivity "android:orientation=" vertical "> <edittext android:id=" @+id/et_name "android:l Ayout_width= "Match_parent" android:layout_height= "wrap_content" android:hint= "Please enter user name"/> <Ed Ittext android:id= "@+id/et_pass" android:layout_width= "match_parent" android:layout_height= "Wrap_cont Ent "android:inputtype=" Textpassword "android:hint=" Please enter the password "/> <relativelayout androi D:layout_width= "Match_parent" android:layout_height= "wrap_content" android:orientation= "horizontal" ><checkbox android:id= "@+id/cb" Android:la Yout_width= "Wrap_content" android:layout_height= "Wrap_content" android:text= "Remember user name and password" Android:layout_centerverti Cal= "true"/><button android:layout_width= "wrap_content" android:layout_height= "Wrap_content" android:l Ayout_gravity= "right" android:text= "login" android:layout_alignparentright= "true" android:onclick= "login"/>&L T;/relativelayout></linearlayout>
Next is the code for the main interface:
Package Com.itydl.rwinrom;import Java.io.bufferedreader;import Java.io.file;import java.io.fileinputstream;import Java.io.filenotfoundexception;import Java.io.fileoutputstream;import Java.io.inputstreamreader;import Org.apache.http.entity.inputstreamentity;import Com.itheima.rwinsd.r;import Android.os.bundle;import Android.os.environment;import Android.annotation.suppresslint;import Android.app.activity;import Android.view.menu;import Android.view.view;import Android.widget.checkbox;import Android.widget.EditText;import Android.widget.toast;public class Mainactivity extends Activity {private EditText et_name;private EditText et_pass; @Ov Erride protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Et_name = (EditText) Findviewbyid (r.id.et_name); Et_pass = (EditText) Findviewbyid (R.id.et_pass); Readaccount (); } public void Readaccount () {if (environmEnt.getexternalstoragestate (). Equals (environment.media_mounted)) {File File = new file ("Sdcard/info.txt"); if (file.exists ()) {try {FileInputStream FIS = new FileInputStream (file);//convert byte streams to character stream BufferedReader br = new Bufferedrea Der (FIS);//Read the user name and password in the txt file string text = Br.readline (); String[] s = Text.split ("# #"); Et_name.settext (S[0]); Et_pass.settext (S[1]);} catch (Exception e) {//TODO auto-generated catch Blocke.printstacktrace ();} }}} public void login (View v) {String name = Et_name.gettext (). toString (); String pass = Et_pass.gettext (). toString (); CheckBox cb = (checkbox) Findviewbyid (R.ID.CB); Determines whether the marquee is checked if (cb.ischecked ()) {//environment.getexternalstoragestate () gets the current sdcard status//media_unknown: Unrecognized SD card/ /media_removed: There is no SD card//MEDIA_UNMOUNTED:SD card exists but no Mount//MEDIA_CHECKING:SD card is being prepared//MEDIA_MOUNTED:SD card is already mounted, available if (Envi Ronment.getexternalstoragestate (). Equals (environment.media_mounted)) {//returns aA file object whose path is the real path of the SD card file File = new file (Environment.getexternalstoragedirectory (), "info.txt");//File File = new File ("Sdcard/info.txt"); FileOutputStream fos;try {fos = new FileOutputStream (file); Fos.write ((Name + "# #" + Pass). GetBytes ()); Fos.close ();} catch (Exception e) {//TODO auto-generated catch Blocke.printstacktrace ();} } else{Toast.maketext (this, "SD card is not available Yo Pro", 0). Show (); }}//Create and display the Toast dialog toast.maketext (this, "login succeeded", 0). Show (); } }
Unlike in-memory read and write information, the SD card reads and writes require permissions:
<uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name= "Android.permission.READ_EXTERNAL_STORAGE"/>
Run as follows:
Click Login, after toast. Once again into the interface. The data will be echoed, since the user name and password are saved to the SD file by the tick. Then open the activity interface, directly read the data in the SD file is urgent
Android Simple Combat Tutorial--the 15th session of "read and write files in external storage"