Android---6---save data to SD card

Source: Internet
Author: User

The same is true of the functionality required in the previous article, except this time saving the data to an SD card.


Mainactivity.java


Package Com.example.qqlogin;import Java.util.map;import Com.example.qqlogin.utils.utils;import Com.example.qqlogin.utils.utilsofsdcard;import Android.app.activity;import Android.os.bundle;import Android.text.textutils;import Android.util.log;import Android.view.menu;import Android.view.MenuItem;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.button;import Android.widget.checkbox;import android.widget.edittext;import android.widget.toast;/** * * @author Caesar * */public Class Mainactivity extends Activity implements Onclicklistener {private EditText numbertext;private EditText pswtext; Private Button button;private CheckBox cb;protected void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (r.layout.activity_main); numbertext = (EditText) Findviewbyid (R.id.number); Pswtext = (EditText) Findviewbyid (R.ID.PSW); cb = (CheckBox) Findviewbyid (r.id.remember); button = (Button) Findviewbyid ( R.id.login); Button.setonclicklIstener (this);//Echo Data map<string, string> Userinfomap = Utilsofsdcard.getuserinfo (this); if (userinfomap! = null) { Numbertext.settext (Userinfomap.get ("number"));p Swtext.settext (Userinfomap.get ("PSW"));}} public void OnClick (View v) {String number = Numbertext.gettext (). toString (); String PSW = Pswtext.gettext (). toString (); String Info = "Account" + number + "password" + PSW; Toast.maketext (this, Info, 0). Show (); if (textutils.isempty (number) | | Textutils.isempty (PSW)) {Toast.maketext (this, "Please fill in correctly", 0). Show (); if (cb.ischecked ()) {if (Utilsofsdcard.saveuserinfo (This,number, PSW)) Toast.maketext (this, "Save succeeded", 0). Show (); else { Toast.maketext (This, "Save Failed", 0). Show ();}} Toast.maketext (This, "Login succeeded", 1). Show ();}}

Utilsofsdcard.java



Package Com.example.qqlogin.utils;import Java.io.bufferedreader;import Java.io.file;import Java.io.FileInputStream ; Import Java.io.filenotfoundexception;import Java.io.fileoutputstream;import Java.io.filereader;import Java.io.inputstreamreader;import Java.util.hashmap;import Java.util.map;import Android.content.Context;import Android.os.environment;import android.text.textutils;/** * Save data to SD card * via Environment.getexternalstoragedirectory (); * @author Caesar * */public class Utilsofsdcard {/** * saved to SD card * @param context * @param number * @param PSW * @return */pu Blic Static Boolean Saveuserinfo (context context, String number,string psw) {try {File sdcardfile = Environment.getexterna Lstoragedirectory (); File File = new file (sdcardfile, "data.txt"); FileOutputStream Fos;fos = new FileOutputStream (file); String data = number + "# #" + Psw;fos.write (Data.getbytes ()); Fos.flush (); Fos.close (); return true;} catch (Exception e) {//TODO auto-generated catch Blocke.printstacktrace ();} return false;} /** *Read data from SD card * @param context * @return */public static map<string, string> GetUserInfo (context context) {try {File SDC Ardfile = Environment.getexternalstoragedirectory (); File File = new file (sdcardfile, "data.txt"); BufferedReader br = new BufferedReader (new InputStreamReader (new FileInputStream (file)); String readLine = Br.readline (); if (! Textutils.isempty (ReadLine)) {map<string, string> userinfomap = new hashmap<string, string> (); String split[] = Readline.split ("# #"); Userinfomap.put ("number", split[0]); Userinfomap.put ("PSW", split[1]); return Userinfomap;}} catch (Exception e) {//TODO auto-generated catch Blocke.printstacktrace ();} return null;}}


Actvity_main.xml


<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" fill_parent "android:layout_height=" Fill_parent "Android:orienta tion= "vertical" android:paddingbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_ Horizontal_margin "android:paddingright=" @dimen/activity_horizontal_margin "android:paddingtop=" @dimen/activity_ Vertical_margin "tools:context=" com.example.qqlogin.MainActivity "> <edittext android:layout_width=" fill_p Arent "android:layout_height=" wrap_content "android:id=" @+id/number "android:hint=" QQ number/Mobile phone number/mailbox "/&G     T;<edittext android:layout_width= "fill_parent" android:layout_height= "wrap_content" android:id= "@+id/psw"        Android:inputtype= "Textpassword" android:hint= "password"/><checkbox android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" Android:text= "Remember Password" android:checked= "true" android:id= "@+id/remember"/><button android:layout_width= "wrap _content "android:layout_height=" wrap_content "android:id=" @+id/login "android:text=" Login "/></LinearLayou T>


Another thing is that you need to add permissions to get access to the SD card:

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


Android---6---save data to SD card

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.