1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
public
class
SerializableBitmap
implements
Serializable {
private
static
final
long
serialVersionUID = -5228835919664263905L;
private
Bitmap bitmap;
public
SerializableBitmap(Bitmap b) {
bitmap = b;
}
// Converts the Bitmap into a byte array for serialization
private
void
writeObject(ObjectOutputStream out)
throws
IOException {
ByteArrayOutputStream byteStream =
new
ByteArrayOutputStream();
boolean
success = bitmap.compress(Bitmap.CompressFormat.PNG,
0
, byteStream);
byte
bitmapBytes[] = byteStream.toByteArray();
if
(success)
out.write(bitmapBytes,
0
, bitmapBytes.length);
}
// Deserializes a byte array representing the Bitmap and decodes it
private
void
readObject(ObjectInputStream in)
throws
IOException, ClassNotFoundException {
ByteArrayOutputStream byteStream =
new
ByteArrayOutputStream();
int
b;
while
((b = in.read()) != -
1
)
byteStream.write(b);
byte
bitmapBytes[] = byteStream.toByteArray();
bitmap = BitmapFactory.decodeByteArray(bitmapBytes,
0
, bitmapBytes.length);
}
public
Bitmap getBitmap() {
return
this
.bitmap;
}
}
|
类中要使用Bitmap的时候就用SerializableBitmap,设置就是new SerializableBitmap(Bitmap bitmap),取出就是SerializableBitmap.getBitmap()