gpt4 book ai didi

java - 制作包含字节数组的自定义 Parcelable

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:47:05 32 4
gpt4 key购买 nike

我正在尝试创建一个包含字节数组的 Parcelable 类。我一直在尝试各种各样的事情,但如果我想将 parcelable 放入我的 Intent 中。

public class Car implements Parcelable{

private String numberPlate;
private String objectId;
private byte[] photo;
private String type;
private ParseGeoPoint location;
private ParseUser owner;
private String brand;
private double pricePerHour;
private double pricePerKm;
public static final String TYPES[] = {"Cabrio", "Van", "SUV", "Station", "Sedan", "City", "Different"};


public Car(String numberPlate, byte[] bs, String type, ParseGeoPoint location, String brand) {

this.numberPlate = numberPlate;
this.photo = bs;
this.type = type;
this.brand = brand;
this.setLocation(location);
this.owner = ParseUser.getCurrentUser();
}

public Car(Parcel in){
readFromParcel(in);
}

public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
public Car createFromParcel(Parcel in) {
return new Car(in);
}

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



public int describeContents() {
// TODO Auto-generated method stub
return 0;
}

public void writeToParcel(Parcel dest, int flags) {
dest.writeString(type);
dest.writeString(numberPlate);
dest.writeString(brand);
dest.writeDouble(pricePerHour);
dest.writeDouble(pricePerKm);
dest.writeString(objectId);
dest.writeByteArray(photo);


}

public void readFromParcel(Parcel in){
this.type = in.readString();
this.numberPlate = in.readString();
this.brand = in.readString();
this.pricePerHour = in.readDouble();
this.pricePerKm = in.readDouble();
this.objectId = in.readString();
byte[] ba = in.createByteArray();
in.unmarshall(ba, 0, ba.length);
this.photo = ba;

}

如果我不包含字节数组,它工作正常..

最佳答案

为什么要使用 createByteArray ??我以它应该工作的方式修改了你的代码......

public void writeToParcel(Parcel dest, int flags) {
dest.writeString(type);
dest.writeString(numberPlate);
dest.writeString(brand);
dest.writeDouble(pricePerHour);
dest.writeDouble(pricePerKm);
dest.writeString(objectId);
dest.writeInt(photo.length());
dest.writeByteArray(photo);
}



public void readFromParcel(Parcel in){
this.type = in.readString();
this.numberPlate = in.readString();
this.brand = in.readString();
this.pricePerHour = in.readDouble();
this.pricePerKm = in.readDouble();
this.objectId = in.readString();
this.photo = new byte[in.readInt()];
in.readByteArray(this.photo);
}

关于java - 制作包含字节数组的自定义 Parcelable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10898116/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com