大家都知道Bitmap没有实现Serializable接口,如果想通过序列化的方式把Bitmap传递到下个目标的时候会报没有序列化异常,比如有一个实体里面有一个Bitmap的属性,通过Bundle类的putSerializable()方法把实体传递到下一个Activity的时候就会报刚才所说的异常,那怎么办呢,办法总是会有的,可以通过把Bitmap转换成字节数组,同时把存放位图的Bitmap属性替换成byte[],在需要显示位图的时候即可通过把字节数组转换回来Bitmap.
public class BookMarkBean implements Serializable{
private static final long serialVersionUID=7981560250804078637l;
String bookMarkUrl;
String bookMarkName;
String bookMarkTitle;
byte[] bookMarkImgBytes;
public byte[] getBookMarkImgBytes() {
return bookMarkImgBytes;
}
public void setBookMarkImgBytes(byte[] bookMarkImgBytes) {
this.bookMarkImgBytes = bookMarkImgBytes;
}
public void setBookMarkImg(Bitmap bookMarkImg){
//实例化字节数组输出流
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bookMarkImg.compress(Bitmap.CompressFormat.PNG, 0, baos);//压缩位图
setBookMarkImgBytes(baos.toByteArray());
}
public Bitmap getBookMarkImg() {
return BitmapFactory.decodeByteArray(getBookMarkImgBytes(), 0, getBookMarkImgBytes().length);
}
public String getBookMarkUrl() {
return bookMarkUrl;
}
public void setBookMarkUrl(String bookMarkUrl) {
this.bookMarkUrl = bookMarkUrl;
}
public String getBookMarkName() {
return bookMarkName;
}
public void setBookMarkName(String bookMarkName) {
this.bookMarkName = bookMarkName;
}
public String getBookMarkTitle() {
return bookMarkTitle;
}
public void setBookMarkTitle(String bookMarkTitle) {
this.bookMarkTitle = bookMarkTitle;
}
}