这次主要是对SD卡外部存储和SharedPreferences存储的补充说明
还是以qq案例来说
首先外部存储
存储的位置为/storage/emulated/0目录下 具体为你定义的地方这里也有个小坑
还有就是不需要使用内部存储的openFileOutput方法和openFileInput方法
首先要在清单文件里申请SD卡写读文件的权限
两种方式静态和动态申请
自然在清单文件里配置静态申请简单了
</application>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
然后看工具类的编写
也就是对内部存储的更改
package com.example.store;
import android.content.Context;
import android.os.Environment;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
//工具类 实现QQ的账号密码存储和读取
public class SDTool_qqAcitivity {
public static boolean saveUser(Context context, String user, String pwd) {
String state = Environment.getExternalStorageState();
if (state.equals(Environment.MEDIA_MOUNTED)) {
// File path = Environment.getExternalStorageDirectory();
File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
// File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS);
File file = new File(path, "haha.txt");
FileOutputStream fos = null;
try {
// fos = context.openFileOutput("haha.txt", Context.MODE_PRIVATE);
fos= new FileOutputStream(file);
//将数据转换为字节码的形式写入haha.txt中
fos.write((user + ":" + pwd).getBytes());
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
}
return true;
} else {
return false;
}
}
public static Map<String, String> getUser(Context context) {
String state = Environment.getExternalStorageState();
if (state.equals(Environment.MEDIA_MOUNTED)) {
// File path = Environment.getExternalStorageDirectory();
File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
File file = new File(path, "haha.txt");
String str = "";
FileInputStream fis = null;
try {
// fis = context.openFileInput("haha.txt");
fis=new FileInputStream(file);
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
str = new String(buffer);
HashMap<String, String> map = new HashMap<String, String>();
String[] split = str.split(":");
map.put("user", split[0]);
map.put("pwd", split[1]);
return map;
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
return null;
}
}
坑坑坑 如果你获取路径写的是如下的活 会实现不了保存密码也不会在文件里找到
// File path = Environment.getExternalStorageDirectory();
你必须给他一个确定的地方
例如:
Filepath=Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
这样文件就保存到DOWNLOADS目录下了
还有个重要的坑
就是我一直想实现不了密码保存
原因是
// Map<String, String> user =SDTool_qqAcitivity.getUser(this);
boolean b = SPTool_qqAcitivity.saveUser(QQ_Activity.this, user, pwd);
你要确定你调用的是哪个工具
然后就是SharedPreferences存储
它主要是一个轻量级的 存储介绍方法吗 都在33里面介绍了 就不赘婿了
然后就是文件存储在:
对的存储的格式为xml
然后咱们看工具类的编写
主要就是调用编辑器方法
package com.example.store;
import android.content.Context;
import android.content.SharedPreferences;
//工具类 实现QQ的账号密码存储和读取
public class SPTool_qqAcitivity {
public static boolean saveUser(Context context, String user, String pwd) {
SharedPreferences sharedPreferences = context.getSharedPreferences("haha", Context.MODE_PRIVATE);
SharedPreferences.Editor edit = sharedPreferences.edit();
edit.putString("user", user);
edit.putString("pwd", pwd);
edit.commit();
return true;
}
public static String getUser(Context context, String key) {
SharedPreferences sharedPreferences = context.getSharedPreferences("haha", Context.MODE_PRIVATE);
String string = sharedPreferences.getString(key, "没找到");
return string;
}
}
注意注意要在主文件中修改你调用的工具同上一样:
略微修改
因为改成了String方法
// Map<String, String> user =SDTool_qqAcitivity.getUser(this);
// user = Tool_qqAcitivity.getUser(this);
// if (user != null) {
// et_account.setText(user.get("user"));
// et_password.setText(user.get("pwd"));
// }
String user = SPTool_qqAcitivity.getUser(this, "user");
String pwd = SPTool_qqAcitivity.getUser(this, "pwd");
et_account.setText(user);
et_password.setText(pwd);
再来个小补充把 就是我33并没有trycatch处理异常
这样是不对的 所以在这里小小补充下代码
package com.example.store;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Environment;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import java.io.IOException;
import java.util.Map;
public class QQ_Activity extends AppCompatActivity {
//初始化
private EditText et_account;
private EditText et_password;
private Button btn_login;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_qq);
et_account = findViewById(R.id.et_account);
et_password = findViewById(R.id.et_password);
btn_login = findViewById(R.id.btn_login);
// Map<String, String> user =SDTool_qqAcitivity.getUser(this);
// user = Tool_qqAcitivity.getUser(this);
// if (user != null) {
// et_account.setText(user.get("user"));
// et_password.setText(user.get("pwd"));
// }
String user = SPTool_qqAcitivity.getUser(this, "user");
String pwd = SPTool_qqAcitivity.getUser(this, "pwd");
et_account.setText(user);
et_password.setText(pwd);
btn_login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String user = et_account.getText().toString().trim();
String pwd = et_password.getText().toString();
//检查是否为空
if (TextUtils.isEmpty(user)) {
Toast.makeText(QQ_Activity.this, "请输入账号", Toast.LENGTH_SHORT).show();
}
if (TextUtils.isEmpty(pwd)) {
Toast.makeText(QQ_Activity.this, "请输入密码", Toast.LENGTH_SHORT).show();
}
Toast.makeText(QQ_Activity.this, "登录成功", Toast.LENGTH_SHORT).show();
//保存用户信息 调用工具
boolean b = SPTool_qqAcitivity.saveUser(QQ_Activity.this, user, pwd);
if (b) {
Toast.makeText(QQ_Activity.this, "保存成功", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(QQ_Activity.this, "保存失败", Toast.LENGTH_SHORT).show();
}
}
});
}
}
欧克了 那个SQLite数据库存储33中已经很清楚的介绍
那三个存储方法算over了 剩下的就是内容提供者和网络存储了