案例演示:把数据存在手机内存/sd卡。
存在手机内存
XML:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.xxhuang.myapplication.MainActivity">
<EditText android:id="@+id/et_number" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入账号"/>
<EditText android:id="@+id/et_password" android:layout_width="match_parent" android:layout_height="wrap_content" android:password="true" android:hint="请输入密码"/> <CheckBox android:id="@+id/ck_remmber" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="记住密码" android:checked="true"/> <Button android:id="@+id/bt_login" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="登录"/> </LinearLayout>
|
MainActivity:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| public class MainActivity extends AppCompatActivity { private EditText etNumber; private EditText etPassword; private Button btLogin; private CheckBox ckRemmber;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
etNumber = (EditText) findViewById(R.id.et_number); etPassword = (EditText) findViewById(R.id.et_password); btLogin = (Button) findViewById(R.id.bt_login); ckRemmber = (CheckBox) findViewById(R.id.ck_remmber);
Map<String,String> loadMap = Util.load(); if (loadMap != null){ etNumber.setText(loadMap.get("number")); etPassword.setText(loadMap.get("password")); }
etNumber.setSelection(etNumber.length());
btLogin.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String number = etNumber.getText().toString(); String password = etPassword.getText().toString();
if (TextUtils.isEmpty(number) || TextUtils.isEmpty(password)){ Toast.makeText(MainActivity.this,"输入框不能为空",Toast.LENGTH_SHORT).show(); }
if (ckRemmber.isChecked() && !TextUtils.isEmpty(number) && !TextUtils.isEmpty(password)){ boolean isSuccess = Util.save(number, password); if (isSuccess){ Toast.makeText(MainActivity.this,"保存成功",Toast.LENGTH_SHORT).show(); }else { Toast.makeText(MainActivity.this,"保存失败",Toast.LENGTH_SHORT).show(); } } } });
} }
|
Util类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| public class Util{ public static boolean save(String number,String password){ try{ String path = "/data/data/com.example.xxhuang.myapplication/huang.text"; FileOutputStream fos = new FileOutputStream(path);
String data = number + "##" +password;
fos.write(data.getBytes());
fos.flush(); fos.close(); return true; }catch (Exception e){ e.printStackTrace(); } return false; }
public static Map<String,String> load() { try { String path = "/data/data/com.example.xxhuang.myapplication/huang.text"; FileInputStream fis = new FileInputStream(path); BufferedReader buffer = new BufferedReader(new InputStreamReader(fis)); String s = buffer.readLine();
if (!TextUtils.isEmpty(s)) {
String[] sqlit = s.split("##"); Map<String, String> loadMap = new HashMap<String, String>();
loadMap.put("number", sqlit[0]); loadMap.put("password", sqlit[1]); return loadMap; }
}catch(Exception e){ e.printStackTrace(); } return null; }
}
|
这样写,每次更改包名地址,path链接都需要改变,优化写法:
Util类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| public static boolean save(Context context ,String number,String password){ try{ File fileDir = context.getFilesDir(); File file = new File(fileDir,"xxhuang.text");
FileOutputStream fos = new FileOutputStream(file);
...
|
1 2 3 4 5 6 7
| public static Map<String,String> load(Context context) {
try {
File fileDir = context.getFilesDir(); File file = new File(fileDir,"xxhuang.text"); FileInputStream fis = new FileInputStream(file);
|
MainActivity
1 2 3
| Map<String,String> loadMap = Util.load(this); boolean isSuccess = Util.save(MainActivity.this, number, password);
|
存在sd卡
得到sd卡的文件路径对象就是(其他和存储在手机内存是一样的意思)。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| public class Util{ public static boolean save(Context context ,String number,String password){ try{ String state = Environment.getExternalStorageState(); if (!Environment.MEDIA_MOUNTED.equals(state)){ return false; }
File sdCard = Environment.getExternalStorageDirectory();
File file = new File(sdCard,"xxhuang.text");
FileOutputStream fos = new FileOutputStream(file);
|
1 2 3 4 5 6 7 8 9 10 11 12 13
| public static Map<String,String> load(Context context) {
try { String state = Environment.getExternalStorageState(); if (!Environment.MEDIA_MOUNTED.equals(state)){ return null; }
File sdCard = Environment.getExternalStorageDirectory(); File file = new File(sdCard,"xxhuang.text");
FileInputStream fis = new FileInputStream(file); BufferedReader buffer = new BufferedReader(new InputStreamReader(fis));
|
添加往sd卡写入的权限。
1
| <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
|