我正在为android开发一个小配方管理工具。对于数据存储,我使用的是ORMLite库。现在我正在尝试将图像作为字节数组保存到数据库中。不知怎的,这不起作用,我无法弄清楚原因。
这是类的构造函数,字节数组传递给
public class RecipeTable {
@DatabaseField(generatedId = true)
int id;
@DatabaseField
String cocktailName;
@DatabaseField
int addition1;
@DatabaseField
int addition2;
@DatabaseField
int addition3;
@DatabaseField
int addition4;
@DatabaseField
int addition5;
@DatabaseField
int addition6;
@DatabaseField (dataType = DataType.BYTE_ARRAY, columnName = "yo")
byte[] imageBytes;
@DatabaseField
Date date;
public RecipeTable(){
}
public RecipeTable(String cocktailName, int addition1, int addition2, int addition3, int addition4, int addition5, int addition6, byte[] imageBytesa) {
super();
this.cocktailName = cocktailName;
this.addition1 = addition1;
this.addition2 = addition2;
this.addition3 = addition3;
this.addition4 = addition4;
this.addition5 = addition5;
this.addition6 = addition6;
this.imageBytes = imageBytesa;
this.date = new Date(System.currentTimeMillis());
}
@Override
public String toString() {
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("id", id);
jsonObject.put("cocktailName", cocktailName);
jsonObject.put("addition1", addition1);
jsonObject.put("addition2", addition2);
jsonObject.put("addition3", addition3);
jsonObject.put("addition4", addition4);
jsonObject.put("addition5", addition5);
jsonObject.put("addition6", addition6);
jsonObject.put("imageByte", imageBytes);
} catch (JSONException e) {
e.printStackTrace();
}
return jsonObject.toString();
}
public byte[] getImageBytes(){return imageBytes;}
public int getId(){return id;}
public String getAddition1(){return "yihha";}
}
在另一个类中,我正在创建RecipeTable的对象
recipeDao.create(new RecipeTable(etCocktailName.getText().toString(), additionAmounts[0],additionAmounts[1],additionAmounts[2],additionAmounts[3],
additionAmounts[4],additionAmounts[5],imageByte));
像这样创建imageByte
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 70, stream);
imageByte = stream.toByteArray();
当我试图获取所创建对象的imageByte值时,它的值为null。 (recipeTable.get(0).getImageBytes()) 这是我的输出显示的内容(Log.d):
D / imageByte:[ - 119,90,78,71,13,10,26,10,0,0,0,13,73,72 ......
D /食谱内容:: [{“id”:1,“cocktailName”:“x”,“addition1”:1,“addition2”:1,“addition3”:1,“addition4”:1, “addition5”:11 “addition6”:1}]
缺少imageByte。我认为没有正确传递
提前致谢