关闭

android 之 SharedPreferences 保存对象数据

103人阅读 评论(0) 收藏 举报
分类:

通常我们认为Sharedpreferences能够保存简单的数据类型 int double float等等,其实它也能保存一个对象,不过需要先将对象进行Base64编码后才能保存到Sharedpreferences中,我封装了一个类

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import org.apache.commons.codec.binary.Base64;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.StreamCorruptedException;

public class SharedPreferencesUtil {
    private static Context mContext;
    private static SharedPreferencesUtil sp = new SharedPreferencesUtil();
    private static SharedPreferences saveInfo;
    private static Editor saveEditor;

    public static SharedPreferencesUtil getInstance(Context context){

        mContext =context;
        if (saveInfo == null && mContext != null) {
            saveInfo = mContext.getSharedPreferences("News",Context.MODE_PRIVATE);
            saveEditor = saveInfo.edit();
        }
        return sp;
    }
    private SharedPreferencesUtil() {
        if (mContext != null) {
            saveInfo = mContext.getSharedPreferences("News",Context.MODE_PRIVATE);
            saveEditor = saveInfo.edit();

        }
    }

    /**
     * @param key
     * @param value
     */
    public void saveSerializable(String key, Object value) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            ObjectOutputStream oos = new ObjectOutputStream(baos);
            oos.writeObject(value);
            oos.flush();
            if (oos != null)
                oos.close();
            String strData = new String(Base64.encodeBase64(baos.toByteArray()));
            saveEditor.putString(key, strData);
            saveEditor.commit();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (baos != null)
                    baos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * @param key
     * @return
     */
    public Object restoreSerializable(String key) {
        Object obj = null;
        String objectInfo = saveInfo.getString(key, null);
        if (objectInfo == null) {
            return null;
        }
        ByteArrayInputStream bais = new ByteArrayInputStream(Base64.decodeBase64(objectInfo.getBytes()));
        try {
            ObjectInputStream ois = new ObjectInputStream(bais);
            obj = ois.readObject();
            ois.close();
        } catch (StreamCorruptedException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            try {
                if (bais != null)
                    bais.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return obj;
    }

通过单例模式产生一个Utils工具,可以通过Utils对需要保存的对象进行保存,获取保存的对象等等操作

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:25214次
    • 积分:324
    • 等级:
    • 排名:千里之外
    • 原创:18篇
    • 转载:2篇
    • 译文:0篇
    • 评论:3条