android - 如何使用 Android 中的 Parcelable 接口(interface)通过 Intent 将包含可绘制参数的对象传递给另一个 Activity

标签 android android-intent android-activity drawable parcelable

我想使用 Parcelable Inteface 将包含可绘制字段的对象传递给另一个 Activity。但问题是,在 public void writeToParcel(Parcel Parcel, int flags) 类中的重写方法(其对象必须发送),我不能有“parcel.writeDrawable”类型的方法。所以其他字段我可以通过,但不能通过 Drawable。 实现 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);
}

并使用以下代码将我的对象放入 Intent

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

那么我必须做什么才能通过那个可绘制字段。

我知道我可以使用共享首选项、SD 卡、全局应用程序类、静态字段;但我不想用它。因为将来我的代码可能会改变,所以我想要健壮的代码。

最佳答案

将您的 Drawable 转换为位图并将其发送到另一个 Activity。

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

获取该 Intent

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

关于android - 如何使用 Android 中的 Parcelable 接口(interface)通过 Intent 将包含可绘制参数的对象传递给另一个 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13179024/

相关文章:

android - 在相机 Activity 期间按后退按钮停止应用程序

java - Activity 关闭时服务停止

java - 无法启动新 Activity

android - 如何改变 native 布局方向?

android - PhoneGap Android从3.7.0到4.0.2跨域XHR 404的

android - 是 Intent.FLAG_ACTIVITY_REORDER_TO_FRONT 的任何替代

android - 如何将我的应用程序的通知声音添加到通知声音列表?

java - 简单的 Android Activity 导致崩溃

android - onPrepareOptionsMenu 未被调用

Android - 可扩展 View 控件