这是一个可以实现读取写入内存卡数据
public class ReadInterStorage extends AppCompatActivity implements View.OnClickListener{
private EditText editText_account;
private EditText editText_key;
private Button button_login;
private CheckBox checkBox_remberKey;
private long blockSize;
private long availableBlocks;
Button button_getAvail;
TextView textView_getAvail;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_read_inter_storage);
editText_account = (EditText)findViewById(R.id.editText_account);
editText_key = (EditText)findViewById(R.id.editText_key);
button_login = (Button)findViewById(R.id.button_login);
checkBox_remberKey = (CheckBox) findViewById(R.id.checkbox_remberKey);
button_getAvail = (Button)findViewById(R.id.button_getAvail);
textView_getAvail = (TextView) findViewById(R.id.textView_avail);
readAccount();
button_login.setOnClickListener(this);
button_getAvail.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button_login:
String name = editText_account.getText().toString();
String key = editText_key.getText().toString();
//判断checkBox是否勾选
if (checkBox_remberKey.isChecked()) {
//定义内部存储空间的路径
//File file = new File("data/data/com.esioner.practise.ReadStorage/info.txt");
//通过api接口获得文件路径
// File file = new File(getFilesDir(),"info.txt");
//mediaRemove:没有SD卡 //mediaUnkonw:不能识别SD卡
// mediaUnkonwed:SD卡存在但是没有挂载// mediaCheckING:SD卡正在准备
// mediaMounted:SD卡已经挂载,可用
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
//把文件写入到sd卡
File file = new File(Environment.getExternalStorageDirectory(), "info.txt");
FileOutputStream fos;
try {
fos = new FileOutputStream(file);
fos.write((name + "##" + key).getBytes());
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
Toast.makeText(ReadInterStorage.this, "恭喜登陆成功", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(ReadInterStorage.this, "SD卡不可用", Toast.LENGTH_LONG).show();
}
}
break;
case R.id.button_getAvail:
getAvail();
Toast.makeText(ReadInterStorage.this,"textView_getAvail",Toast.LENGTH_LONG).show();
break;
}
}
public void readAccount() {
//File file = new File("data/data/com.esioner.practise.ReadStorage/info.txt");
File file = new File(Environment.getExternalStorageDirectory(),"info.txt");
FileInputStream fis;
if (file.exists()) {
try {
fis = new FileInputStream(file);
//把字节流转换成字符流
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
//读取文件里的账号和密码
String text = br.readLine();
String[] s = text.split("##");
editText_account.setText(s[0]);
editText_key.setText(s[1]);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
//获取当前手机可用内存
public void getAvail(){
//获取当前手机的sd卡内存
File path = Environment.getExternalStorageDirectory();
StatFs stat = new StatFs(path.getPath());
//获取当前系统版本的等级
if(Build.VERSION.SDK_INT>=18) {//获取当前手机版本号
//当版本号大于等于18,(安卓4.3)就用新的方法
blockSize = stat.getBlockSizeLong();
availableBlocks = stat.getAvailableBlocksLong();
}else{
blockSize = stat.getBlockSize();
availableBlocks = stat.getAvailableBlocks();
}
textView_getAvail.setText(formatSize(availableBlocks*blockSize));
}
private String formatSize(long size){
return Formatter.formatFileSize(this,size);
}
}
Android自学中,留下笔记。