标签:
在实际开发是需要使用路径api来获取外部存储的真实盘符路径,因为各大厂商的sd路径各不相同,为了兼容,在开发是需要使用api来获取sd路径的。
Environment.getExternalStorageDirectory() 返回包含sd卡真实路径的File对象。
我们在将数据保存到sd中之前需要判断sd卡的状态是否能够进行读写操作
Environment.getExternalStorageState() 返回String类型字符串
1 /** 2 * 登陆 3 * 4 * @param view 5 */ 6 public void login(View view) { 7 String name = et_name.getText().toString(); 8 String pass = et_pass.getText().toString(); 9 10 CheckBox cb = (CheckBox) findViewById(R.id.cb); 11 if (cb.isChecked()) { 12 if (Environment.MEDIA_MOUNTED.equals(Environment 13 .getExternalStorageState())) { 14 File file = new File(Environment.getExternalStorageDirectory(), 15 "info.txt"); 16 try { 17 FileOutputStream fos = new FileOutputStream(file); 18 fos.write((name + "##" + pass).getBytes()); 19 Toast.makeText(this, "登陆成功", 0).show(); 20 fos.close(); 21 } catch (Exception e) { 22 Toast.makeText(this, "登陆失败", 0).show(); 23 e.printStackTrace(); 24 } 25 } 26 } else { 27 Toast.makeText(this, "亲,你没有记住用户名和密码啊", 0).show(); 28 } 29 } 30 31 /** 32 * 记住用户名和密码 33 */ 34 public void readAccount() { 35 File file = new File(Environment.getExternalStorageDirectory(), 36 "info.txt"); 37 FileInputStream fis; 38 if (file.exists()) { 39 try { 40 fis = new FileInputStream(file); 41 BufferedReader br = new BufferedReader(new InputStreamReader( 42 fis)); 43 String text = br.readLine(); 44 String[] strings = text.split("##"); 45 et_name.setText(strings[0]); 46 et_pass.setText(strings[1]); 47 } catch (Exception e) { 48 e.printStackTrace(); 49 } 50 } else { 51 Toast.makeText(this, "没有找到文件", 0).show(); 52 } 53 }
标签:
原文地址:http://www.cnblogs.com/xinjianwenjianjia/p/4540370.html