Android进阶——Sharedpreferences保存对象和图⽚等复杂类型的数据
Android进阶——Sharedpreferences保存对象和图⽚等复杂类型的数据
⼀、保存和读取基本数据类型
1、保存基本类型的数据
/**
* 根据传⼊的prefereces的⽂件名设置指定key-valeu
*@param context
* @param preferenceName SharedPreferences的name
* @param key 对应的Key键
* @param object 对应的各种类型的值
*/
public static void saveKeyValue(Context context, String preferenceName, String key, Object object){ SharedPreferences
SharedPreferences(preferenceName,context.MODE_PRIV ATE); SharedPreferences.Editor editor=sharedPreferences.edit();
if (object instanceof String) {
editor.putString(key, (String) object);
} else if (object instanceof Integer) {
editor.putInt(key, (Integer) object);
} else if (object instanceof Boolean) {对象图片高清
editor.putBoolean(key, (Boolean) object);
} else if (object instanceof Float) {
editor.putFloat(key, (Float) object);
} else if (object instanceof Long) {
editor.putLong(key, (Long) object);
} else {
editor.putString(key, String());
}
www.doczj/doc/df6fa1f4162ded630b1c59eef8c75fbfc77d9496.html mit();
}
2、读取基本类型的数据
/**
* 根据传⼊的prefereces的⽂件名设置指定key-valeu
*@param context
* @param preferenceName SharedPreferences的name
* @param key 对应的Key键
* @param object 对应的各种类型的值
*/
public static Object getValueByKey(Context context, String preferenceName,String key,Object object){ SharedPreferences
SharedPreferences(preferenceName,context.MODE_PRIV ATE);
if (object instanceof String) {
String(key, (String) object);
} else if (object instanceof Integer) {
Int(key, (Integer) object);
} else if (object instanceof Boolean) {
Boolean(key, (Boolean) object);
} else if (object instanceof Float) {
Float(key, (Float) object);
} else if (object instanceof Long) {
Long(key, (Long) object);
} else {
String(key, String());
}
}
⼆、保存和读取图⽚
SharedPreferences原则上只能将字符串以key-value的形式保存,但是万物皆⼆进制,所以我们可以采
⽤编码的⽅式将任何⼆进制数据转化为字符串,从⽽将可以将⼆进制数据保存在SharedPreferences⽂件中,⽽最常⽤的编码格式是Base64.
1、保存⼆进制数据
/**
* @param context
* @param preferenceName
* @param resId
* @param key
*/
public static void saveDrawable(Context context, String preferenceName,int resId,String key) {
SharedPreferences
SharedPreferences(preferenceName,context.MODE_PRIV ATE); SharedPreferences.Editor editor=sharedPreferences.edit();
Bitmap bitmap = BitmapFactory.Resources(), resId);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
www.doczj/doc/df6fa1f4162ded630b1c59eef8c75fbfc77d9496.html
press(www.doczj/doc/df6fa1f4162ded630b1c59eef8c75fbfc77d9496.html pressFormat.PNG, 50, baos);
String imageBase64 = new
ByteArray(),Base64.DEFAULT));
editor.putString(key,imageBase64 );
www.doczj/doc/df6fa1f4162ded630b1c59eef8c75fbfc77d9496.html mit();
}
2、读取⼆进制数据
public static Drawable getDrawableByKey(Context context, String preferenceName,String key) {
SharedPreferences
SharedPreferences(preferenceName,context.MODE_PRIV ATE);
String www.doczj/doc/df6fa1f4162ded630b1c59eef8c75fbfc77d9496.html emp = String(key, "");
ByteArrayInputStream bais = new ByteArrayInputStream(Base64.Bytes(), Base64.DEFAULT));
ateFromStream(bais, "");
}
三、保存和读取序列化的对象
由于⼆进制数据经过编码后可以⽤SharedPreferences以字符串的形式存储,所以保存对象也成为了可能,但是这个类必须是可序列化即implements Serializable(实际上Serializable
接⼝是个空接⼝,只是为了标记该对象是被序列化的),然后可以通过ObjectOutputStream 保存再转为⼆进制存储。
1、保存序列化的对象
/**
* @param user
*/
public static void saveUser(Context context, String preferenceName,String key,User user) throws Exception {
if(user instanceof Serializable) {
SharedPreferences sharedPreferences = SharedPreferences(preferenceName, context.MODE_PRIVATE); SharedPreferences.Editor editor = sharedPreferences.edit();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(user);//把对象写到流⾥
String temp = new ByteArray(), Base64.DEFAULT));
editor.putString(key, temp);
www.doczj/doc/df6fa1f4162ded630b1c59eef8c75fbfc77d9496.html mit();
} catch (IOException e) {
e.printStackTrace();
}
}else {
throw new Exception("User must implements Serializable");
}
}
2、读取序列化的对象
public static User getUser(Context context, String preferenceName,String key) {
SharedPreferences
SharedPreferences(preferenceName,context.MODE_PRIVATE);
String temp = String(key, "");
ByteArrayInputStreambais=new
ByteArrayInputStream(Base64.Bytes(), Base64.DEFAULT));
User user = null;
try {
ObjectInputStream ois = new ObjectInputStream(bais);
user = (User) adObject();
} catch (IOException e) {
}catch(ClassNotFoundException e1) {
}
return user;
}
当然Sharedpreferences也是可以存储各种集合类的⽐如说List,都可以通过转为ObjectOutputStream输出流进⽽编码存储:public static String listToString(List list)throws IOException {
// 实例化⼀个ByteArrayOutputStream对象,⽤来装载压缩后的字节⽂件。
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
// 然后将得到的字符数据装载到ObjectOutputStream
ObjectOutputStream objectOutputStream = new ObjectOutputStream(
byteArrayOutputStream);
// writeObject ⽅法负责写⼊特定类的对象的状态,以便相应的readObject ⽅法可以还原它
objectOutputStream.writeObject(list);
// 最后,⽤de将字节⽂件转换成Base64编码保存在String中
String listString = new String(www.doczj/doc/df6fa1f4162ded630b1c59eef8c75fbfc77d9496.de( ByteArray(), Base64.DEFAULT));
// 关闭objectOutputStream
objectOutputStream.close();
return listString;
}
@SuppressWarnings("unchecked")
public static List StringToList(String listString) throws StreamCorruptedException, IOException, ClassNotFoundException {
byte[] mobileBytes = Base64.Bytes(),
Base64.DEFAULT);
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(
mobileBytes);
ObjectInputStream objectInputStream = new ObjectInputStream(
byteArrayInputStream);
List WeatherList = (List) objectInputStream
.readObject();
objectInputStream.close();
return WeatherList;
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。