当这个错误发生时,我正在尝试将一个json转换回一个对象。我怎样才能摆脱这个错误?这是从json转换后我尝试获取其对象的类:
class Recognition implements Serializable {
private final String id;
private final String title;
private final Float distance;
private Object extra;
private RectF location;
private Integer color;
private Bitmap crop;
public Recognition(
final String id, final String title, final Float distance, final RectF location) {
this.id = id;
this.title = title;
this.distance = distance;
this.location = location;
this.color = null;
this.extra = null;
this.crop = null;
}
public void setExtra(Object extra) {
this.extra = extra;
}
public Object getExtra() {
return this.extra;
}
public void setColor(Integer color) {
this.color = color;
}
public String getId() {
return id;
}
public String getTitle() {
return title;
}
public Float getDistance() {
return distance;
}
public RectF getLocation() {
return new RectF(location);
}
public void setLocation(RectF location) {
this.location = location;
}
public Integer getColor() {
return this.color;
}
public void setCrop(Bitmap crop) {
this.crop = crop;
}
public Bitmap getCrop() {
return this.crop;
}
@Override
public String toString() {
String resultString = "";
if (id != null) {
resultString += "[" + id + "] ";
}
if (title != null) {
resultString += title + " ";
}
if (distance != null) {
resultString += String.format("(%.1f%%) ", distance * 100.0f);
}
if (location != null) {
resultString += location + " ";
}
if (color != null) {
resultString += color + " ";
}
if (extra != null) {
resultString += extra + " ";
}
if (crop != null) {
resultString += encodeToBase64(crop, Bitmap.CompressFormat.JPEG, 100) + " ";
}
return resultString.trim();
}
public static String encodeToBase64(Bitmap image, Bitmap.CompressFormat compressFormat, int quality)
{
ByteArrayOutputStream byteArrayOS = new ByteArrayOutputStream();
image.compress(compressFormat, quality, byteArrayOS);
return Base64.encodeToString(byteArrayOS.toByteArray(), Base64.DEFAULT);
}
public static Bitmap decodeBase64(String input)
{
byte[] decodedBytes = Base64.decode(input, 0);
return BitmapFactory.decodeByteArray(decodedBytes, 0, decodedBytes.length);
}
private void writeObject(java.io.ObjectOutputStream out) throws IOException {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
crop.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
out.writeInt(byteArray.length);
out.write(byteArray);
}
private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException {
int bufferLength = in.readInt();
byte[] byteArray = new byte[bufferLength];
int pos = 0;
do {
int read = in.read(byteArray, pos, bufferLength - pos);
if (read != -1) {
pos += read;
} else {
break;
}
} while (pos < bufferLength);
crop = BitmapFactory.decodeByteArray(byteArray, 0, bufferLength);
}
}
我认为在构造器或位图转换中可能存在一些问题。但我不确定。所以如果你有什么解决办法的话。请告诉我。
暂无答案!
目前还没有任何答案,快来回答吧!