【问题标题】:Parcel Bitmap - Android包裹位图 - Android
【发布时间】:2015-03-06 04:12:29
【问题描述】:

在现有代码中,我有以下类来保存类别详细信息。

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;
}

}

应用程序工作正常,但现在我想将 Image 属性添加到这个类别类。我是 Java 和 Android 的新手。但是现有的类有一个叫做包裹的东西。现在我如何为位图做同样的事情?是不是像下面这样

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.

}

请指导我

【问题讨论】:

    标签: java android


    【解决方案1】:
    public void writeToParcel(Parcel dest, int flags) {
    ...
        dest.writeValue(mImage);
    }
    private Category(Parcel in) {
    ...
        mImage= in.readParcelable(Bitmap.class.getClassLoader());
    }
    

    即使mImage==null 也可以。

    【讨论】:

    • 03-06 01:17:53.643:E/Parcel(15238):解组时找不到类:��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) ): 在 java.lang.Class.forName(Class.java:251) 03-06 01:17:53.643: E/Parcel(15238):
    • 使用writeParcelable() 而不是writeValue()
    【解决方案2】:

    简单尝试

    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];
        }
    
      };
    }
    

    简单发送

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

    只需根据您的需要更改“MyVeryOwnParcelable”,我就会指出需要更改的位置

    希望这会对某人有所帮助

    【讨论】:

      【解决方案3】:

      你有一个例子here

      mImage.writeToParcel(parcel, 0);
      

      【讨论】:

      • 关于接收意图,parcel.setDataPosition(0); destinationBitmap = Bitmap.CREATOR.createFromParcel(parcel);
      猜你喜欢
      • 1970-01-01
      • 2014-07-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-01
      • 1970-01-01
      相关资源
      最近更新 更多