外部sd卡读取数据需要权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<!-- hint就是类似于qq输入上的阴影字体 -->
<EditText
android:id="@+id/et_number"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="请输入QQ号"
/>
<!-- 密文输入 android:inputType="textPassword" -->
<EditText
android:id="@+id/et_password"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:hint="密码"
/>
<!-- android:checked="true"设定默认选中 -->
<CheckBox
android:id="@+id/cb_remerber_pwd"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:checked="true"
android:text="记住密码"
/>
<Button
android:id="@+id/bt_login"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="登陆"
/>
</LinearLayout>
package com.heima.qq;
import java.io.File;
import java.util.Map;
import com.heima.utils.Utils;
import com.heima.utils.UtilsOfSdcard;
import com.heima.utils.UtilsOfSharedPreferences;
import android.app.Activity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.StatFs;
import android.text.TextUtils;
import android.text.format.Formatter;
import android.util.Log;
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 Qq_loginActivity extends Activity implements OnClickListener{
private static final String TAG = "Qq_loginActivity";
private CheckBox cbrememberPWD;
private EditText etnumber;
private EditText etpassword;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// //自动动态获取包名
// this.getFilesDir();
Button button = (Button) findViewById(R.id.bt_login);
cbrememberPWD = (CheckBox) findViewById(R.id.cb_remerber_pwd);
etnumber = (EditText) findViewById(R.id.et_number);
etpassword = (EditText) findViewById(R.id.et_password);
button.setOnClickListener(this);
//回显数据
Map<String, String> userInfoMap = UtilsOfSharedPreferences.getUserInfo(this);
if(userInfoMap != null){
etnumber.setText(userInfoMap.get("name"));
etpassword.setText(userInfoMap.get("password"));
}
}
public void onClick(View v) {
// TODO Auto-generated method stub
//执行登陆的操作
Log.w("qqlog", "login");
//取出号码和密码
String number = etnumber.getText().toString();
String password = etpassword.getText().toString();
if(TextUtils.isEmpty(number) || TextUtils.isEmpty(password)){
//吐丝的使用
//第一参数是那个窗口前使用 第二参数是输入内容 第三参数是输出时间长短
Toast toast = Toast.makeText(this, "请正确输入", Toast.LENGTH_SHORT);
toast.show();
return;
}
//判断是否选中记住密码
if(cbrememberPWD.isChecked()){
Log.i(TAG, "记住密码: "+number+" and "+password);
if(UtilsOfSharedPreferences.saveUserInfo(this,number, password)){
//保存成功
Toast.makeText(this, "保存密码成功", 0).show();
}else{
Toast.makeText(this, "保存密码失败", 0).show();
}
}
//登陆成功 0代表的是时间短的意思
Toast.makeText(this, "登陆成功", 0).show();
}
}
存放的是手机自带有的空间
getFilesDir 根据上下文获取文件路径
package com.heima.utils;
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 java.util.HashMap;
import java.util.Map;
import android.content.Context;
import android.text.TextUtils;
//存放的是手机自带有的空间
public class Utils {
public static boolean saveUserInfo(Context context ,String name,String password){
//路径
// String path = "/data/data/com.heima.qq/itheima28.txt";
//getFilesDir 根据上下文获取文件路径
File filesDir = context.getFilesDir();
File f = new File(filesDir, "itheima28.txt");
try {
FileOutputStream fos = new FileOutputStream(f);
//账号和密码用##连接,使得只有一条记录
String data = name+"##"+password;
fos.write(data.getBytes());
fos.flush();
fos.close();
return true;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
public static Map<String, String> getUserInfo(Context context){
//包名不要写死了
// String path = "/data/data/com.heima.qq/itheima28.txt";
File fileDir = context.getFilesDir();
File f = new File(fileDir, "itheima28.txt");
try {
FileInputStream fis = new FileInputStream(f);
//字符流转化为字节流
BufferedReader reader = new BufferedReader(new InputStreamReader(fis));
String text = reader.readLine();
reader.close();
if(!TextUtils.isEmpty(text)){
String[] split = text.split("##");
Map<String, String> map = new HashMap<String, String>();
map.put("name", split[0]);
map.put("password", split[1]);
return map;
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
存放的是sd卡
需考虑sd卡是否满了或者是否有sd卡的存在
Environment自动获取外部文件的路径
package com.heima.utils;
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 java.util.HashMap;
import java.util.Map;
import android.content.Context;
import android.os.Environment;
import android.text.TextUtils;
//存放的是sd卡
public class UtilsOfSdcard {
public static boolean saveUserInfo(Context context ,String name,String password){
//需考虑sd卡是否满了或者是否有sd卡的存在
//判断当前是否有sd卡
String state = Environment.getExternalStorageState();
if(!Environment.MEDIA_MOUNTED.equals(state)){
return false;
}
try {
//Environment自动获取外部文件的路径 也就是sd卡的路径
File sdCardFile = Environment.getExternalStorageDirectory();
File file = new File(sdCardFile, "itheima28.txt");
FileOutputStream fos = new FileOutputStream(file);
String data = name+"##"+password;
fos.write(data.getBytes());
fos.flush();
fos.close();
return true;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
public static Map<String, String> getUserInfo(Context context){
//判断当前是否有sd卡
String state = Environment.getExternalStorageState();
if(!Environment.MEDIA_MOUNTED.equals(state)){
return null;
}
File fileDir = Environment.getExternalStorageDirectory();
File f = new File(fileDir, "itheima28.txt");
try {
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(f)));
String text = br.readLine();
br.close();
if(!TextUtils.isEmpty("text")){
String[] split = text.split("##");
Map<String, String> map = new HashMap<String, String>();
map.put("name", split[0]);
map.put("password", split[1]);
return map;
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
使用SharedPreferences来进行读写,可以设置文件读写权限
默认文件所处位置 data/data/包名/shared_prefs/itheima28 还是在本机上,只有sd卡是外部存储
Editor对象存放数据
package com.heima.utils;
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 java.util.HashMap;
import java.util.Map;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Environment;
import android.text.TextUtils;
//使用SharedPreferences来进行读写
public class UtilsOfSharedPreferences {
public static boolean saveUserInfo(Context context ,String name,String password){
try {
// /data/data/包名/shared_prefs/itheima28 不用加后缀名,默认是xml,加了也是xml
SharedPreferences sp = context.getSharedPreferences("itheima28",
//设置文件的权限类型
context.MODE_PRIVATE);
//获得一个编辑对象
Editor edit = sp.edit();
//存数据
edit.putString("number", name);
edit.putString("password", password);
//上面只是存放在内存 想要真正存在文件中就要提交以下
edit.commit();
return true;
} catch (Exception e) {
// TODO: handle exception
}
return false;
}
public static Map<String, String> getUserInfo(Context context){
SharedPreferences sp = context.getSharedPreferences("itheima28",
context.MODE_PRIVATE);
//取数据不用edit 直接取数据 第二参数是取不出或者没有数据是的默认初始值
String number = sp.getString("number", null);
String password = sp.getString("password", null);
if(!TextUtils.isEmpty(number) && !TextUtils.isEmpty(password)){
Map<String, String> map = new HashMap<String, String>();
map.put("name", number);
map.put("password", password);
return map;
}
return null;
}
}