Android---6---将数据保存到SD卡
同样是实现上一篇文章中所需要的功能,只不过这次是将数据保存到SD卡中。
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); //回显数据 Map<String, String> userInfoMap = utilsOfSDCard.GetUserInfo(this); if (userInfoMap != null){ numberText.setText(userInfoMap.get("number")); pswText.setText(userInfoMap.get("psw")); } } public void onClick(View v) { String number = numberText.getText().toString(); String psw = pswText.getText().toString(); String Info = "账号" + number + "密码" + psw; Toast.makeText(this, Info, 0).show(); if (TextUtils.isEmpty(number) || TextUtils.isEmpty(psw)) { Toast.makeText(this, "请正确填写", 0).show(); } if (cb.isChecked()) { if (utilsOfSDCard.SaveUserInfo(this,number, psw)) Toast.makeText(this, "保存成功", 0).show(); else { Toast.makeText(this,"保存失败", 0).show(); } } Toast.makeText(this, "登陆成功", 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; /** * 保存数据到SD卡 * 通过Environment.getExternalStorageDirectory(); * @author Caesar * */ public class utilsOfSDCard { /** * 保存到SD卡 * @param context * @param number * @param psw * @return */ public static boolean SaveUserInfo(Context context, String number, String psw) { try { File SDCardFile = Environment.getExternalStorageDirectory(); 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 block e.printStackTrace(); } return false; } /** * 从SD卡中读取数据 * @param context * @return */ public static Map<String, String> GetUserInfo(Context context) { try { File sDCardFile = 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 block e.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:orientation="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_parent" android:layout_height="wrap_content" android:id="@+id/number" android:hint="QQ号/手机号/邮箱" /> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/psw" android:inputType="textPassword" android:hint="密码" /> <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="记住密码" android:checked="true" android:id="@+id/remember" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/login" android:text="登陆" /> </LinearLayout>
还有一点就是需要添加权限,须获得读取SD卡的权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。