androidandroid-intentandroid-activitydrawableparcelable

how to pass an object containing with drawable parameter to another activity through intent using Parcelable Interface In android


I want to pass an object containing a drawable field using Parcelable Inteface to another Activity. But problem is, in public void writeToParcel(Parcel parcel, int flags) overridden method in class(whose object has to send), I can't have "parcel.writeDrawable" kind of method. So other fields I'm able to pass, but not Drawable. I have added the following methods in my class after implementing Parcelable Interface.

public static final Parcelable.Creator<ImageInfo> CREATOR = new Parcelable.Creator<ImageInfo>() {

    public ImageInfo createFromParcel(Parcel source) {
        return new ImageInfo(source);
    }

    public ImageInfo[] newArray(int size) {
        return new ImageInfo[size];
    }
};

public int describeContents() {
    return 1;
}

public void writeToParcel(Parcel parcel, int flags) {
    parcel.writeString(name);   
    parcel.writeString(price);
    parcel.writeString(urlOfImage);
}

and using following code I'm putting my object in intent

Bundle b = new Bundle();
b.putParcelable("parse", imageInfo);
intentTrackIntHome.putExtras(b);

So what I have to do to pass that drawable field.

I know I can use shared Preferences, sd card, global application class, static fields; but I don't want to use it. Because in future my code can change, so I want robust code.


Solution

  • Convert your Drawable to Bitmap and send it to Another Activity.

    Bitmap bitmap = ((BitmapDrawable)d).getBitmap();
    intent.putExtra("Bitmap", bitmap);
    

    To Fetch, that intent

    Bitmap bitmap = (Bitmap)this.getIntent().getParcelableExtra("Bitmap");