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

谁说胖子不能爱 提交于 2019-12-11 04:15:41

问题


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:


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



来源:https://stackoverflow.com/questions/13179024/how-to-pass-an-object-containing-with-drawable-parameter-to-another-activity-thr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!