Android平台实现位图(Bitmap)的序列化和反序列化

  对象被序列化后可被保存或传输,这个例子实现了位图的序列化和反序列化。  

  这次就对Android工程自带的”icon.png”进行下手。

  程序运行后会在/data/data/breakan.serializable/目录下生成一个bitmap.bin文件,这个文件保存的就是MyBitmap的对象。

  

 

 

 

 

 

1 package breakan.serializable;
2
3  import java.io.ByteArrayOutputStream;
4  import java.io.FileInputStream;
5  import java.io.FileOutputStream;
6  import java.io.ObjectInputStream;
7  import java.io.ObjectOutputStream;
8  import java.io.Serializable;
9
10  import android.app.Activity;
11  import android.graphics.Bitmap;
12  import android.graphics.BitmapFactory;
13  import android.graphics.Bitmap.CompressFormat;
14  import android.graphics.drawable.BitmapDrawable;
15  import android.os.Bundle;
16  import android.widget.ImageView;
17  import android.widget.TextView;
18
19  public class SerializableActivity extends Activity {
20 private Bitmap bitmap = null;
21 private ObjectOutputStream oos = null;
22 private ObjectInputStream ois = null;
23 // myBitmap1是要被序列化的对象
24 private MyBitmap myBitmap1 = null;
25 // myBitmap2是反序列化后得到的对象
26 private MyBitmap myBitmap2 = null;
27 private TextView tv1 = null;
28 private ImageView img1 = null;
29
30 /** Called when the activity is first created. */
31 @Override
32 public void onCreate(Bundle savedInstanceState) {
33 super.onCreate(savedInstanceState);
34 setContentView(R.layout.main);
35 bitmap = ((BitmapDrawable) getResources().getDrawable(R.drawable.icon))
36 .getBitmap();
37 myBitmap1 = new MyBitmap(BytesBitmap.getBytes(bitmap), "icon.png");
38 tv1 = (TextView) findViewById(R.id.tv1);
39 img1 = (ImageView) findViewById(R.id.img1);
40
41 try {
42 // 序列化myBitmap对象
43 oos = new ObjectOutputStream(new FileOutputStream(
44 "/data/data/breakan.serializable/bitmap.bin"));
45 oos.writeObject(myBitmap1);
46 oos.flush();
47 // 反序列化myBitmap对象
48 ois = new ObjectInputStream(new FileInputStream(
49 "/data/data/breakan.serializable/bitmap.bin"));
50 myBitmap2 = (MyBitmap) ois.readObject();
51 } catch (Exception e) {
52 e.printStackTrace();
53 } finally {
54 try {
55 if (oos != null) {
56 oos.close();
57 }
58 if (ois != null) {
59 ois.close();
60 }
61 } catch (Exception e) {
62 e.printStackTrace();
63 }
64 }
65
66 tv1.setText(myBitmap2.getName());
67 img1.setImageBitmap(BytesBitmap.getBitmap(myBitmap2.getBitmapBytes()));
68 }
69
70 }
71
72 /**
73 * 因为Bitmap没有实现序列化,所以不能直接在序列化类(MyBitmap)中使用
74 * BytesBitmap用于实现Bitmap和byte[]间的相互转换
75 * @author joran
76 *
77 */
78 class BytesBitmap {
79 public static Bitmap getBitmap(byte[] data) {
80 return BitmapFactory.decodeByteArray(data, 0, data.length);
81 }
82
83 public static byte[] getBytes(Bitmap bitmap) {
84 ByteArrayOutputStream baops = new ByteArrayOutputStream();
85 bitmap.compress(CompressFormat.PNG, 0, baops);
86 return baops.toByteArray();
87 }
88 }
89
90 /**
91 * MyBitmap是要被序列化的类
92 * 其中包含了通过BytesBitmap类得到的Bitmap中数据的数组
93 * 和一个保存位图的名字的字符串,用于标识图片
94 * @author joran
95 *
96 */
97 class MyBitmap implements Serializable {
98 /**
99 * serialVersionUID解释:
100 * http://www.blogjava.net/invisibletank/archive/2007/11/15/160684.html
101 */
102 private static final long serialVersionUID = 1L;
103 private byte[] bitmapBytes = null;
104 private String name = null;
105
106 public MyBitmap(byte[] bitmapBytes, String name) {
107 // TODO Auto-generated constructor stub
108 this.bitmapBytes = bitmapBytes;
109 this.name = name;
110 }
111
112 public byte[] getBitmapBytes() {
113 return this.bitmapBytes;
114 }
115
116 public String getName() {
117 return this.name;
118 }
119 }

posted on 2011-06-23 16:21  breakan  阅读(4295)  评论(1编辑  收藏  举报

导航