废话少说,上代码,供以后查考
工具类
package com.aa.tst;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.StreamCorruptedException;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.Base64;
public class Util {
public static final String MY_PREFERENCE = "pf";
private static void paraCheck(SharedPreferences sp, String key) {
if (sp == null) {
throw new IllegalArgumentException();
}
if (TextUtils.isEmpty(key)) {
throw new IllegalArgumentException();
}
}
public static boolean putBitmap(Context context, String key, Bitmap bitmap) {
SharedPreferences sp = context.getSharedPreferences(MY_PREFERENCE,
Context.MODE_PRIVATE);
paraCheck(sp, key);
if (bitmap == null || bitmap.isRecycled()) {
return false;
} else {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 100, baos);
String imageBase64 = new String(Base64.encode(baos.toByteArray(),
Base64.DEFAULT));
Editor e = sp.edit();
e.putString(key, imageBase64);
return e.commit();
}
}
public static Bitmap getBitmap(Context context, String key,
Bitmap defaultValue) {
SharedPreferences sp = context.getSharedPreferences(MY_PREFERENCE,
Context.MODE_PRIVATE);
paraCheck(sp, key);
String imageBase64 = sp.getString(key, "");
if (TextUtils.isEmpty(imageBase64)) {
return defaultValue;
}
byte[] base64Bytes = Base64.decode(imageBase64.getBytes(),
Base64.DEFAULT);
ByteArrayInputStream bais = new ByteArrayInputStream(base64Bytes);
Bitmap ret = BitmapFactory.decodeStream(bais);
if (ret != null) {
return ret;
} else {
return defaultValue;
}
}
public static boolean putDrawable(Context context, String key, Drawable d) {
SharedPreferences sp = context.getSharedPreferences(MY_PREFERENCE,
Context.MODE_PRIVATE);
paraCheck(sp, key);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
((BitmapDrawable) d).getBitmap()
.compress(CompressFormat.JPEG, 50, baos);
String imageBase64 = new String(Base64.encode(baos.toByteArray(),
Base64.DEFAULT));
Editor e = sp.edit();
e.putString(key, imageBase64);
return e.commit();
}
public static Drawable getDrawable(Context context, String key,
Drawable defaultValue) {
SharedPreferences sp = context.getSharedPreferences(MY_PREFERENCE,
Context.MODE_PRIVATE);
paraCheck(sp, key);
String imageBase64 = sp.getString(key, "");
if (TextUtils.isEmpty(imageBase64)) {
return defaultValue;
}
byte[] base64Bytes = Base64.decode(imageBase64.getBytes(),
Base64.DEFAULT);
ByteArrayInputStream bais = new ByteArrayInputStream(base64Bytes);
Drawable ret = Drawable.createFromStream(bais, "");
if (ret != null) {
return ret;
} else {
return defaultValue;
}
}
public static boolean putObject(Context context, String key, Object obj) {
SharedPreferences sp = context.getSharedPreferences(MY_PREFERENCE,
Context.MODE_PRIVATE);
paraCheck(sp, key);
if (obj == null) {
Editor e = sp.edit();
e.putString(key, "");
return e.commit();
} else {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = null;
try {
oos = new ObjectOutputStream(baos);
oos.writeObject(obj);
} catch (IOException e1) {
e1.printStackTrace();
return false;
}
String objectBase64 = new String(Base64.encode(baos.toByteArray(),
Base64.DEFAULT));
Editor e = sp.edit();
e.putString(key, objectBase64);
return e.commit();
}
}
public static Object getObject(Context context, String key,
Object defaultValue) {
SharedPreferences sp = context.getSharedPreferences(MY_PREFERENCE,
Context.MODE_PRIVATE);
paraCheck(sp, key);
String objectBase64 = sp.getString(key, "");
if (TextUtils.isEmpty(objectBase64)) {
return defaultValue;
}
byte[] base64Bytes = Base64.decode(objectBase64.getBytes(),
Base64.DEFAULT);
ByteArrayInputStream bais = new ByteArrayInputStream(base64Bytes);
ObjectInputStream ois;
try {
ois = new ObjectInputStream(bais);
return ois.readObject();
} catch (StreamCorruptedException e) {
// e.printStackTrace();
return null;
} catch (IOException e) {
// e.printStackTrace();
return null;
} catch (ClassNotFoundException e) {
// e.printStackTrace();
return null;
}
}
public static boolean isObjectEqual(Context context, String key,
Object newValue) {
SharedPreferences sp = context.getSharedPreferences(MY_PREFERENCE,
Context.MODE_PRIVATE);
paraCheck(sp, key);
if (newValue == null) {
return false;
} else {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = null;
try {
oos = new ObjectOutputStream(baos);
oos.writeObject(newValue);
} catch (IOException e1) {
e1.printStackTrace();
return false;
}
String newValueBase64 = new String(Base64.encode(
baos.toByteArray(), Base64.DEFAULT));
String oldValueBase64 = sp.getString(key, "");
return newValueBase64.equals(oldValueBase64);
}
}
}
用法:
package com.aa.tst;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends Activity {
int count = 0;
private Context context;
private ImageView img;
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context = this;
setContentView(R.layout.activity_main);
img = (ImageView) findViewById(R.id.show_img);
tv = (TextView) findViewById(R.id.show_obj);
tv.setText("");
// 存图片
SharedPreferUtils.putBitmap(this, "pic", BitmapFactory.decodeResource(getResources(), R.drawable.appicon));
View pic = findViewById(R.id.obtain_pic);
pic.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Bitmap bitmap = SharedPreferUtils.getBitmap(context, "pic", BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher));
img.setImageBitmap(bitmap);
}
});
Obj obj = new Obj();
obj.age = 33;
obj.name = "SingleDog";
// 存对象
SharedPreferUtils.putObject(context, "obj", obj);
View findViewById = findViewById(R.id.obtain_obj);
findViewById.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Object object = SharedPreferUtils.getObject(context, "obj", "NULL");
tv.setText(object.toString());
}
});
}
}
注意,存储对象时,对象要实现serializable接口
package com.aa.tst;
import java.io.Serializable;
public class Obj implements Serializable {
private static final long serialVersionUID = -7204021997306230979L;
public String name;
public int age = 200;
@Override
public String toString() {
return "Obj [name=" + name + ", age=" + age + "]";
}
}