how to pass an object containing with drawable par

2019-07-21 00:11发布

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.

1条回答
一夜七次
2楼-- · 2019-07-21 01:01

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");

For More detail Follow this Link

查看更多
登录 后发表回答