记住:
getFilesDir()方法是获取手机内存的路径 /data/data/包名/files 自动建立一个files文件夹
Environment.getExternalStorageDirectory获取的是sd卡的路径, 自动获取 /mnt/card 不同的手机的路径可能有差异,所以要用 Environment获取/mnt...../card
Environment.getDataDirectory(); 获取的手机内存的根路径: /data
//判断当前的手机是否有sd卡
String state = Environment.getExternalStorageState();
if(!Environment.MEDIA_MOUNTED.equals(state)){
//已经挂载
}
package com.itheima28.qqlogin;
import java.io.File;
import java.util.Map;
import com.itheima28.qqlogin.utils.Utils;
import com.itheima28.qqlogin.utils.UtilsOfSDCard;
import com.itheima28.qqlogin.R;
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;
public class MainActivity extends Activity implements OnClickListener {
private static final String TAG = "MainActivity";
private EditText etNumber;
private EditText etPassword;
private CheckBox cbRemerberPWD;
private Map<String, String> userInfoMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// /data/data/包名/files
/*
this.getFilesDir-- /data/data/com.itheima28.qqlogin/files
*/
File file = this.getFilesDir();
System.out.println(file.toString());
etNumber = (EditText) findViewById(R.id.et_number);
etPassword = (EditText) findViewById(R.id.et_password);
cbRemerberPWD = (CheckBox) findViewById(R.id.cb_remember_pwd);
Button btnLogin = (Button) findViewById(R.id.btn_login);
btnLogin.setOnClickListener(this);
//回显
userInfoMap = UtilsOfSDCard.getUserInfo(this);
if(userInfoMap != null){
etNumber.setText(userInfoMap.get("number"));
etPassword.setText(userInfoMap.get("password"));
}
}
@Override
public void onClick(View v) {
//执行登录的方法
//1.取出号码和密码
String number = etNumber.getText().toString();
String password = etPassword.getText().toString();
if(TextUtils.isEmpty(number)||TextUtils.isEmpty(password)){
//Toast--弹出吐司,下面的业务就不执行了
Toast.makeText(this, "请正确输入", Toast.LENGTH_SHORT).show();
/*
第一个参数为Context对象,MainActivity继承Context,所以用this就行了
第二个参数时候提示的信息
第三个参数持续的时间 Toast.LENGTH_SHORT--0秒 Toast.LENGTH_LONG--1秒
*/
}
//2.判断记住密码是否被选中,如果被选中,存起来
if(cbRemerberPWD.isChecked()){
//当前需要记住密码
Log.i(TAG, "记住密码:"+number + ","+password);
boolean isSuccess = UtilsOfSDCard.saveUserInfo(this,number, password);
if(isSuccess){
Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(this, "保存失败", Toast.LENGTH_SHORT).show();
}
}
//3.登录成功
Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();
}
/*
* 4.0以后的问题
读sc卡不加权限出现的问题:
W/System.err(6323): java.io.FileNotFoundException: /mnt/sdcard/ithema28.txt: open failed: EACCES (Permission denied)
需要加读写权限
*/
}
package com.itheima28.qqlogin.utils;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import android.content.Context;
import android.os.Environment;
import android.text.TextUtils;
public class UtilsOfSDCard {
/**
* 保存用户信息到sd卡
* */
public static boolean saveUserInfo(Context context,String number,String password){
try {
//判断当前的手机是否有sd卡
String state = Environment.getExternalStorageState();
if(!Environment.MEDIA_MOUNTED.equals(state)){
//已经挂载
}
/*
自动获取 /mnt/card 不同的手机的路径可能有差异,所以要用 Environment获取/mnt...../card
* */
File sdCardFile = Environment.getExternalStorageDirectory();
File file = new File(sdCardFile, "ithema28.txt");//指定存储的文件名字
FileOutputStream fos = new FileOutputStream(file);
String data = number + "##" + password;
fos.write(data.getBytes());
fos.flush();
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
/**
* 到sd卡获取用户信息
* */
public static Map<String,String> getUserInfo(Context context){
try {
//判断当前的手机是否有sd卡
String state = Environment.getExternalStorageState();
if(!Environment.MEDIA_MOUNTED.equals(state)){
//已经挂载
}
File sdCardFile = Environment.getExternalStorageDirectory();
File file = new File(sdCardFile, "ithema28.txt");//指定存储的文件名字
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
String text = br.readLine();
if(!TextUtils.isEmpty(text)){
Map<String,String> userInfoMap = new HashMap<String,String>();
String[] split = text.split("##");
userInfoMap.put("number", split[0]);
userInfoMap.put("password", split[1]);
}
} catch (Exception e) {
}
return null;
}
}
<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:orientation="vertical" >
<EditText
android:id="@+id/et_number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入QQ号"
/>
<EditText
android:id="@+id/et_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:hint="请输入密码"/>
<CheckBox
android:id="@+id/cb_remember_pwd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="记住密码"/>
<!-- checkedtrue代表选中 -->
<Button
android:id="@+id/btn_login"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="登录"/>
</LinearLayout>