java - Make custom Parcelable containing byte array -
i'm trying make parcelable class contains byte array. have been trying sorts of things, still seems fail if want put 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; } it works fine if don't include byte array..
why use createbytearray ?? revised code in way should work...
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); }
Comments
Post a Comment