Sharepreference存储对象,图片

废话少说,上代码,供以后查考
工具类
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 + "]";
	}

}



  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值