Parcel Bitmap - Android
android, java
Solution
public void writeToParcel(Parcel dest, int flags) {
...
dest.writeValue(mImage);
}
private Category(Parcel in) {
...
mImage= in.readParcelable(Bitmap.class.getClassLoader());
}
This will work even if `mImage==null`.
Problem
In the existing code I have following class to hold category details. ``` import org.json.JSONObject; import android.graphics.Bitmap; import android.os.Parcel; import android.os.Parcelable; public class Category { private String mIconURL; private String mName; public Category() { super(); } public String getIconURL() { return mIconURL; } public void setIconURL(String iconURL) { this.mIconURL = iconURL; } public String getName() { return mName; } public void setName(String name) { this.mName = name; } @Override public void writeToParcel(Parcel dest, int flags) { super.writeToParcel(dest, flags); dest.writeString(mIconURL); dest.writeString(mName); } public static final Parcelable.Creator<Category> CREATOR = new Parcelable.Creator<Category>() { public Category createFromParcel(Parcel in) { return new Category(in); } public Category[] newArray(int size) { return new Category[size]; } }; private Category(Parcel in) { super(in); mIconURL = in.readString(); mName = in.readString(); } @Override public int describeContents() { return 1; } @Override public String toString() { StringBuilder builder = new StringBuilder(); builder.append("{"); builder.append(super.toString()); builder.append(", mName=").append(mName); builder.append(", mIconURL=").append(mIconURL); builder.append("}"); return builder.toString(); } public static Category parseFromJSON(JSONObject jsonObject) { Category cat = new Category(); try { cat.setServerId(jsonObject.getInt("id")); cat.setName(jsonObject.getString("name")); cat.setIconURL(jsonObject.getString("icon")); } catch (Exception e) { } return cat; } ``` } And application works fine, but now I want to add Image property to this category class. I am new to Java and Android. But existing class has something called parcel. Now how i do same for bitmap? Is it like below ``` public Bitmap getImage() { return mImage; } @Override public void writeToParcel(Parcel dest, int flags) { super.writeToParcel(dest, flags); dest.writeString(mIconURL); dest.writeString(mName); dest.writeByteArray(mImage);// there is no "writeBitmap" method in Parcel. } ``` Please guide me