使用Sharepreference存储对象,图片

来源:互联网 发布:联通网络在线客服 编辑:程序博客网 时间:2024/04/26 00:49
  1. package com.aa.tst;  
  2.   
  3. import java.io.ByteArrayInputStream;  
  4. import java.io.ByteArrayOutputStream;  
  5. import java.io.IOException;  
  6. import java.io.ObjectInputStream;  
  7. import java.io.ObjectOutputStream;  
  8. import java.io.StreamCorruptedException;  
  9.   
  10. import android.content.Context;  
  11. import android.content.SharedPreferences;  
  12. import android.content.SharedPreferences.Editor;  
  13. import android.graphics.Bitmap;  
  14. import android.graphics.BitmapFactory;  
  15. import android.graphics.Bitmap.CompressFormat;  
  16. import android.graphics.drawable.BitmapDrawable;  
  17. import android.graphics.drawable.Drawable;  
  18. import android.text.TextUtils;  
  19. import android.util.Base64;  
  20.   
  21. public class Util {  
  22.     public static final String MY_PREFERENCE = "pf";  
  23.   
  24.     private static void paraCheck(SharedPreferences sp, String key) {  
  25.         if (sp == null) {  
  26.             throw new IllegalArgumentException();  
  27.         }  
  28.         if (TextUtils.isEmpty(key)) {  
  29.             throw new IllegalArgumentException();  
  30.         }  
  31.     }  
  32.   
  33.     public static boolean putBitmap(Context context, String key, Bitmap bitmap) {  
  34.         SharedPreferences sp = context.getSharedPreferences(MY_PREFERENCE,  
  35.                 Context.MODE_PRIVATE);  
  36.   
  37.         paraCheck(sp, key);  
  38.         if (bitmap == null || bitmap.isRecycled()) {  
  39.             return false;  
  40.         } else {  
  41.             ByteArrayOutputStream baos = new ByteArrayOutputStream();  
  42.             bitmap.compress(CompressFormat.PNG, 100, baos);  
  43.             String imageBase64 = new String(Base64.encode(baos.toByteArray(),  
  44.                     Base64.DEFAULT));  
  45.             Editor e = sp.edit();  
  46.             e.putString(key, imageBase64);  
  47.             return e.commit();  
  48.         }  
  49.     }  
  50.   
  51.     public static Bitmap getBitmap(Context context, String key,  
  52.             Bitmap defaultValue) {  
  53.         SharedPreferences sp = context.getSharedPreferences(MY_PREFERENCE,  
  54.                 Context.MODE_PRIVATE);  
  55.   
  56.         paraCheck(sp, key);  
  57.         String imageBase64 = sp.getString(key, "");  
  58.         if (TextUtils.isEmpty(imageBase64)) {  
  59.             return defaultValue;  
  60.         }  
  61.   
  62.         byte[] base64Bytes = Base64.decode(imageBase64.getBytes(),  
  63.                 Base64.DEFAULT);  
  64.         ByteArrayInputStream bais = new ByteArrayInputStream(base64Bytes);  
  65.         Bitmap ret = BitmapFactory.decodeStream(bais);  
  66.         if (ret != null) {  
  67.             return ret;  
  68.         } else {  
  69.             return defaultValue;  
  70.         }  
  71.     }  
  72.   
  73.     public static boolean putDrawable(Context context, String key, Drawable d) {  
  74.         SharedPreferences sp = context.getSharedPreferences(MY_PREFERENCE,  
  75.                 Context.MODE_PRIVATE);  
  76.         paraCheck(sp, key);  
  77.         ByteArrayOutputStream baos = new ByteArrayOutputStream();  
  78.         ((BitmapDrawable) d).getBitmap()  
  79.                 .compress(CompressFormat.JPEG, 50, baos);  
  80.         String imageBase64 = new String(Base64.encode(baos.toByteArray(),  
  81.                 Base64.DEFAULT));  
  82.         Editor e = sp.edit();  
  83.         e.putString(key, imageBase64);  
  84.         return e.commit();  
  85.     }  
  86.   
  87.     public static Drawable getDrawable(Context context, String key,  
  88.             Drawable defaultValue) {  
  89.         SharedPreferences sp = context.getSharedPreferences(MY_PREFERENCE,  
  90.                 Context.MODE_PRIVATE);  
  91.         paraCheck(sp, key);  
  92.         String imageBase64 = sp.getString(key, "");  
  93.         if (TextUtils.isEmpty(imageBase64)) {  
  94.             return defaultValue;  
  95.         }  
  96.   
  97.         byte[] base64Bytes = Base64.decode(imageBase64.getBytes(),  
  98.                 Base64.DEFAULT);  
  99.         ByteArrayInputStream bais = new ByteArrayInputStream(base64Bytes);  
  100.         Drawable ret = Drawable.createFromStream(bais, "");  
  101.         if (ret != null) {  
  102.             return ret;  
  103.         } else {  
  104.             return defaultValue;  
  105.         }  
  106.     }  
  107.   
  108.     public static boolean putObject(Context context, String key, Object obj) {  
  109.         SharedPreferences sp = context.getSharedPreferences(MY_PREFERENCE,  
  110.                 Context.MODE_PRIVATE);  
  111.         paraCheck(sp, key);  
  112.         if (obj == null) {  
  113.             Editor e = sp.edit();  
  114.             e.putString(key, "");  
  115.             return e.commit();  
  116.         } else {  
  117.             ByteArrayOutputStream baos = new ByteArrayOutputStream();  
  118.             ObjectOutputStream oos = null;  
  119.             try {  
  120.                 oos = new ObjectOutputStream(baos);  
  121.                 oos.writeObject(obj);  
  122.             } catch (IOException e1) {  
  123.                 e1.printStackTrace();  
  124.                 return false;  
  125.             }  
  126.             String objectBase64 = new String(Base64.encode(baos.toByteArray(),  
  127.                     Base64.DEFAULT));  
  128.             Editor e = sp.edit();  
  129.             e.putString(key, objectBase64);  
  130.             return e.commit();  
  131.         }  
  132.     }  
  133.   
  134.     public static Object getObject(Context context, String key,  
  135.             Object defaultValue) {  
  136.         SharedPreferences sp = context.getSharedPreferences(MY_PREFERENCE,  
  137.                 Context.MODE_PRIVATE);  
  138.         paraCheck(sp, key);  
  139.         String objectBase64 = sp.getString(key, "");  
  140.         if (TextUtils.isEmpty(objectBase64)) {  
  141.             return defaultValue;  
  142.         }  
  143.         byte[] base64Bytes = Base64.decode(objectBase64.getBytes(),  
  144.                 Base64.DEFAULT);  
  145.         ByteArrayInputStream bais = new ByteArrayInputStream(base64Bytes);  
  146.         ObjectInputStream ois;  
  147.         try {  
  148.             ois = new ObjectInputStream(bais);  
  149.             return ois.readObject();  
  150.         } catch (StreamCorruptedException e) {  
  151.             // e.printStackTrace();  
  152.             return null;  
  153.         } catch (IOException e) {  
  154.             // e.printStackTrace();  
  155.             return null;  
  156.         } catch (ClassNotFoundException e) {  
  157.             // e.printStackTrace();  
  158.             return null;  
  159.         }  
  160.     }  
  161.   
  162.     public static boolean isObjectEqual(Context context, String key,  
  163.             Object newValue) {  
  164.         SharedPreferences sp = context.getSharedPreferences(MY_PREFERENCE,  
  165.                 Context.MODE_PRIVATE);  
  166.         paraCheck(sp, key);  
  167.         if (newValue == null) {  
  168.             return false;  
  169.         } else {  
  170.             ByteArrayOutputStream baos = new ByteArrayOutputStream();  
  171.             ObjectOutputStream oos = null;  
  172.             try {  
  173.                 oos = new ObjectOutputStream(baos);  
  174.                 oos.writeObject(newValue);  
  175.             } catch (IOException e1) {  
  176.                 e1.printStackTrace();  
  177.                 return false;  
  178.             }  
  179.             String newValueBase64 = new String(Base64.encode(  
  180.                     baos.toByteArray(), Base64.DEFAULT));  
  181.             String oldValueBase64 = sp.getString(key, "");  
  182.             return newValueBase64.equals(oldValueBase64);  
  183.         }  
  184.     }  
  185. }  
用法:
[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. package com.aa.tst;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.Context;  
  5. import android.graphics.Bitmap;  
  6. import android.graphics.BitmapFactory;  
  7. import android.os.Bundle;  
  8. import android.view.View;  
  9. import android.view.View.OnClickListener;  
  10. import android.widget.ImageView;  
  11. import android.widget.TextView;  
  12.   
  13. public class MainActivity extends Activity {  
  14.     int count = 0;  
  15.     private Context context;  
  16.     private ImageView img;  
  17.     private TextView tv;  
  18.   
  19.     @Override  
  20.     protected void onCreate(Bundle savedInstanceState) {  
  21.         super.onCreate(savedInstanceState);  
  22.         context = this;  
  23.         setContentView(R.layout.activity_main);  
  24.         img = (ImageView) findViewById(R.id.show_img);  
  25.         tv = (TextView) findViewById(R.id.show_obj);  
  26.         tv.setText("");  
  27.         // 存图片  
  28.         SharedPreferUtils.putBitmap(this"pic", BitmapFactory.decodeResource(getResources(), R.drawable.appicon));  
  29.           
  30.         View pic = findViewById(R.id.obtain_pic);  
  31.         pic.setOnClickListener(new OnClickListener() {  
  32.   
  33.             @Override  
  34.             public void onClick(View v) {  
  35.                 Bitmap bitmap = SharedPreferUtils.getBitmap(context, "pic", BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher));  
  36.                 img.setImageBitmap(bitmap);  
  37.             }  
  38.         });  
  39.         Obj obj = new Obj();  
  40.         obj.age = 33;  
  41.         obj.name = "SingleDog";  
  42.         // 存对象  
  43.         SharedPreferUtils.putObject(context, "obj", obj);  
  44.         View findViewById = findViewById(R.id.obtain_obj);  
  45.         findViewById.setOnClickListener(new OnClickListener() {  
  46.   
  47.             @Override  
  48.             public void onClick(View v) {  
  49.                 Object object = SharedPreferUtils.getObject(context, "obj""NULL");  
  50.                 tv.setText(object.toString());  
  51.             }  
  52.         });  
  53.           
  54.     }  
  55. }  

注意,存储对象时,对象要实现serializable接口
[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. package com.aa.tst;  
  2.   
  3. import java.io.Serializable;  
  4.   
  5. public class Obj implements Serializable {  
  6.     private static final long serialVersionUID = -7204021997306230979L;  
  7.     public String name;  
  8.     public int age = 200;  
  9.   
  10.     @Override  
  11.     public String toString() {  
  12.         return "Obj [name=" + name + ", age=" + age + "]";  
  13.     }  
  14.   
  15. }  
  16. 本文转载自:http://blog.csdn.net/majunm/article/details/52061678;
  17. 非常感谢博主的分享。
0 0
原创粉丝点击