之前在写一个android一个小项目时,遇到一个问题,当一个实体类中持有一个Bitmap类的对象时,并且该实体类需要进行网络传输时,仅仅实现Serializable会抛出一个异常:
android.os.BadParcelableException: Parcelable protocol requires a Parcelable.Creator object called CREATOR on class com.txrj.sms.model.TxrjContact
这个异常了之后才知道,Bitmap是android 中的类,默认该类是实现了Parcelable的,并且这个接口不同于java中提供的Serializable。
Serializable,即串行化,或序列化,这是一个标记接口,其作用是将数据对象写入字节流中,必要时可从字节流取出对象,这就是对象的序列化,可用于将数据存储到外存中或者用于网络传输。而android中则提供了Parcelable这个接口,该接口并不像Serializable那样是个标记接口,Parcelable需要实现方法以实现数据能够高效地传输。实现该接口的类的对象可以存储到Parcel中或写入到Parcel中,由于android的系统资源有限,提供Parcelable接口就是为了提高性能,Parcelable接口比Serializable接口性能更高。
下面是实现Parcelable的接口的方法:
package cn.edu.aust.enjoychat.entity;import java.io.Serializable;import java.util.Date;import android.graphics.Bitmap;import android.os.Parcel;import android.os.Parcelable;public class ContactItem implements Parcelable,Serializable{ private static final long serialVersionUID = 1L; // 用户名 private String contactName = ""; // 用户头像图片类型 private String iconType = "jpg"; // 用户头像的图片存储问题 private Bitmap image; // 出生日期 private String birthday; // 昵称 private String nickname = ""; // 性别 private String gender = ""; // 地址 private String address = ""; // 所在地区 private String areaLocation = ""; // 个性签名 private String signature = ""; // 邮箱 private String emial = ""; //联系人拥有者名称 private String ownerName; //联系人经纬度信息 private Position position; public String getOwnerName() { return ownerName; } public Position getPosition() { return position; } public void setPosition(Position position) { this.position = position; } public void setOwnerName(String ownerName) { this.ownerName = ownerName; } public String getContactName() { return contactName; } public void setContactName(String contactName) { this.contactName = contactName; } public String getIconType() { return iconType; } public void setIconType(String iconType) { this.iconType = iconType; } public Bitmap getImage() { return image; } public void setImage(Bitmap image) { this.image = image; } public String getBirthday() { return birthday; } public void setBirthday(String birthday) { this.birthday = birthday; } public String getNickname() { return nickname; } public void setNickname(String nickname) { this.nickname = nickname; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getAreaLocation() { return areaLocation; } public void setAreaLocation(String areaLocation) { this.areaLocation = areaLocation; } public String getSignature() { return signature; } public void setSignature(String signature) { this.signature = signature; } public String getEmial() { return emial; } public void setEmial(String emial) { this.emial = emial; } public static final Parcelable.CreatorCREATOR = new Creator () { public ContactItem createFromParcel(Parcel source) { ContactItem item = new ContactItem(); item.contactName = source.readString(); item.iconType = source.readString(); item.image = Bitmap.CREATOR.createFromParcel(source); item.birthday = source.readString(); item.nickname = source.readString(); item.gender = source.readString(); item.address = source.readString(); item.areaLocation = source.readString(); item.signature = source.readString(); item.emial = source.readString(); return item; } public ContactItem[] newArray(int size) { return new ContactItem[size]; } }; @Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel dest, int flags) { dest.writeString(contactName); dest.writeString(iconType); image.writeToParcel(dest, 0); dest.writeString(birthday); dest.writeString(nickname); dest.writeString(gender); dest.writeString(address); dest.writeString(areaLocation); dest.writeString(signature); dest.writeString(emial); } }