方法一.保存文件路径的方式对文件进行存储
前面是将数据保存到应用的内部:文件路径:/data/user/0/com.example.qqlogindemo/files
1)通过点击事件处理
2)HandlerLoginEvent()方法事件处理
/**
* 处理登录事件
*/
private void handlerLoginEvent(View v) {
//拿到界面上输入的账号和密码
String passwordText = password.getText().toString();
String accountText = account.getText().toString();
//将账号和密码保存起来
SaveUserInfo(passwordText,accountText);
}
private void SaveUserInfo(String passwordText, String accountText) {
//获取文件保存路径
File filesDir = this.getFilesDir();
//获得缓存文件保存路径
File cache = this.getCacheDir();
Log.d(TAG,"缓存文件路径"+cache.toString());
Log.d(TAG,"文件路径"+filesDir.toString());
File saveFile = new File(filesDir,"info.text");
try {
if(!saveFile.exists()){
saveFile.createNewFile();
}
FileOutputStream fos = new FileOutputStream(saveFile);
//存储信息
fos.write(("账户:"+accountText +"密码"+passwordText).getBytes());
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
对数据进行判空检查
①使用TextUtils.isEmpty()方法
//判断密码不许空
if(TextUtils.isEmpty(accountText)){
Toast.makeText(this,"账号不许为空!",Toast.LENGTH_SHORT).show();
return;
}
//判断账号不许为空
if(TextUtils.isEmpty(passwordText)){
Toast.makeText(this,"密码不许为空!",Toast.LENGTH_LONG).show();
return;
}
②使用字符串长度判空
if(passwordText.length() == 0){
Toast.makeText(this,"密码不许为空!",Toast.LENGTH_LONG).show();
return;
}
if (accountText.length() == 0){
Toast.makeText(this,"账号不许为空!",Toast.LENGTH_SHORT).show();
return;
}
3)对数据进行读出操作
①将数据读出时,首先重写Android的生命周期函数onResume(),将保存在文本文件中的方法读出
②将保存的数据进行分割,分割不清楚应先System.out.println()将分割的字符串打印出来
③将所得到的正确账号信息写入
方法二.安卓持久化存储
D/MainActivity: 缓存文件路径
/data/user/0/com.example.qqlogindemo/cache
//获取文件保存路径
File filesDir = this.getFilesDir();
//获得缓存文件保存路径
File cache = this.getCacheDir();
Android上面有两部分存储空间,一部分内部存储空间:ROM,例如Window电脑的系统盘,二是外置存储空间,也就是SD卡,类似于电脑的外插存储设备。
解决SD卡内容不能写入的问题:转载回答
Android 6.0以后的版本报错:open failed: EACCES (Permission denied)
SD卡的路径获取:通过API获取
//拿到SD卡的路径
File externalStorageDirectory = Environment.getExternalStorageDirectory();
检查是否有sd卡挂载
public void onClick(View v) {
if (v == check) {
//提供的API判断是否有sd卡挂载
String state = Environment.getExternalStorageState();
if (state.equals(Environment.MEDIA_MOUNTED)) {
Log.d(TAG, "SD卡已经挂载!");
} else if (state.equals(Environment.MEDIA_UNMOUNTED)){
Log.d(TAG, "SD卡尚未挂载!");
}
}
}
});
获取SD卡的大小
public void onClick(View v) {
File directory = Environment.getExternalStorageDirectory();
long space = directory.getFreeSpace();
String s = Formatter.formatFileSize(this, space);
Log.d(TAG, "剩余空间为:" + s);
}
注意:this实现的是Context父类,用匿名内部类来实现this表示的是new View.OnClickListener(),并未实现activity的方法。
方法三.SharePreferece
一般用于保存条目,比如设置条目,按钮Switch。
sharepreference一般用于储存配置信息,比如说是否是第一次启动呀,这个应用的id号,用户的账号和加密以后的密等等
SharedPreferences保存到哪里去了?
SharedPreferences是以XML的格式以文件的方式自动保存的,在DDMS中的File Explorer中展开到/data/data/<package name>/shared_prefs下,以上面这个为例,可以看到一个叫做SETTING_Infos.xml的文件
SharePreferece也是属于内部存储,跟file/cache一样,在data/data包名下进行保存,以XML形式保存,内容都是以键值对的形式进行保存。
注意:Preferences只能在同一个包内使用,不能在不同的包之间使用。
-使用步骤:
保存数据
1).拿到Sharepreferece
Setting_Info = this.getSharedPreferences("setting_info", MODE_PRIVATE);
this指的就是上下文Context,因为Activity间接继承了Context,所以在Activity里面直接使用this。
2).进入编辑模式,拿到编辑器(在点击事件处理中进行)
SharedPreferences.Editor edit = Setting_Info.edit();
3).保存数据 (保存的数据类型有多种)
edit.putBoolean("state",isChecked);
4).提交编辑器进行保存
edit.commit();
详细过程:
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_preferece_demo);
unSwitch = (Switch)this.findViewById(R.id.unknown_switch);
unSwitch.setOnCheckedChangeListener(this);
Setting_Info = this.getSharedPreferences("setting_info", MODE_PRIVATE);
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Log.d(TAG," "+isChecked);
//保存数据
SharedPreferences.Editor edit = Setting_Info.edit();
edit.putBoolean("state",isChecked);
edit.commit();
}
回显数据:
(在Oncreate()里面进行回显):数据回显和保存均需要getSharedPreference()
//回显数据
Setting_Info = this.getSharedPreferences("setting_info", MODE_PRIVATE);
boolean state = Setting_Info.getBoolean("state", false); //false是默认值
unSwitch.setChecked(state);
数据清除:(每一次实行都得提交数据)
SharedPreferences Setting_Info= getSharedPreferences("Setting_Info", MODE_PRIVATE);
SharedPreferences.Editor edit = userInfo.edit();//获取Editor
edit.remove("state");
edit.commit();
清空所有的数据:(每一次实行都得提交数据)
SharedPreferences Setting_Info= getSharedPreferences("Setting_Info", MODE_PRIVATE);
SharedPreferences.Editor edit = Setting_Info.edit();//获取Editor
edit.clear();
edit.commit();
记得最后commit();