5

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

iShare
  • 125
  • 3
  • 16

4 Answers4

10
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.

SchmitzIT
  • 9,227
  • 9
  • 65
  • 92
sercxjo
  • 328
  • 2
  • 12
  • 1
    03-06 01:17:53.643: E/Parcel(15238): Class not found when unmarshalling: ��an 03-06 01:17:53.643: E/Parcel(15238):java.lang.ClassNotFoundException: ��an 03-06 01:17:53.643: E/Parcel(15238): at java.lang.Class.classForName(Native Method) 03-06 01:17:53.643: E/Parcel(15238): at java.lang.Class.forName(Class.java:251) 03-06 01:17:53.643: E/Parcel(15238): – Ashish Sahu Mar 05 '15 at 20:15
  • Use `writeParcelable()` instead of `writeValue()` – Alaa M. Oct 10 '16 at 21:04
2

Try simply

public class MyVeryOwnParcelable  implements Parcelable {

//you can use every type of data even Arraylist

//change here (1)
public String ID;
public Bitmap BITMAP_IMAGE;

public MyVeryOwnParcelable  () {

}
public MyVeryOwnParcelable  (Parcel parcel) {
    //change here (2)
    this.ID= parcel.readString();
    this.BITMAP_IMAGE=parcel.readParcelable(null);
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    //change here (3)
    dest.writeString(ID);
    dest.writeParcelable(BITMAP_IMAGE, flags);
}

@Override
public int describeContents() {
    return 0;
}
// Method to recreate a Catch from a Parcel
public static Creator<MyVeryOwnParcelable  > CREATOR = new Creator<MyVeryOwnParcelable  >() {

    @Override
    public MyVeryOwnParcelable  createFromParcel(Parcel source) {
        return new MyVeryOwnParcelable  (source);
    }

    @Override
    public MyVeryOwnParcelable  [] newArray(int size) {
        return new MyVeryOwnParcelable  [size];
    }

  };
}

Send simply

MyVeryOwnParcelable mp=new MyVeryOwnParcelable();
mp.ID="hello parcel";
mp.BITMAP_IMAGE=BitmapFactory.decodeResource(getResources(), R.drawable.myimage);

//put as normal
intent.putExtra("MY_DATA",mp);

//get from intent
MyVeryOwnParcelable  my= (MyVeryOwnParcelable )   getIntent().getExtras().getParcelable(KEY.EXTRA_DATA);
//do anything with data

imageView.setImageBitmap(mp.BITMAP_IMAGE);

simply change 'MyVeryOwnParcelable' according your needs l'll already indicate where change require

Hope this will help someone

Ashish Sahu
  • 1,538
  • 17
  • 22
0

You have an example here.

mImage.writeToParcel(parcel, 0);
Community
  • 1
  • 1
Jong
  • 9,045
  • 3
  • 34
  • 66
  • and on the recieving intent, `parcel.setDataPosition(0); destinationBitmap = Bitmap.CREATOR.createFromParcel(parcel);` – IAmGroot Nov 16 '12 at 14:35
0
constructor(parcel: Parcel) : this(
    parcel.readInt(),
    parcel.readString(),
    parcel.readString(),
    parcel.readString(),
    parcel.readInt(),
    parcel.readInt(),
    parcel.readString(),
    parcel.readTypedObject(Bitmap.CREATOR),
    parcel.readTypedObject(Bitmap.CREATOR),
    parcel.readString()
) {
}

override fun describeContents(): Int {
   // TODO("Not yet implemented")
    return 0
}

override fun writeToParcel(p0: Parcel, p1: Int) {
   // TODO("Not yet implemented")
}
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 14 '23 at 12:12