我知道很多次都会问这个问题,但我没有找到适合我情况的解决方案。
public class Recipe implements Serializable {
private static final long serialVersionUID = 1L;
public static final String FIND_ALL = "Recipe.findAll";
public static final String COUNT = "Recipe.count";
private String name;
private String description;
private Integer servings;
private Integer cookingTimeInMin;
private List<Ingredient> ingredients;
private List<Workstep> worksteps;
private RecipeDifficulty recipeDifficulty;
private List<RecipeTag> recipeTags;
private List<Comment> comments;
private EndUser author;
private List<SerializableBitmap> pictures;
...
}
我想传递您在上面看到的类的对象。 Ingredient,Workstep,RecipeDifficulty,RecipeTag,Comment,Enduser和SerializableBitmap都是可序列化的。
一切正常,直到我包含图片。所以这里是我的类SerializableBitmap的片段:
public class SerializableBitmap implements Serializable {
private static final long serialVersionUID = 1L;
private Bitmap bitmap;
public SerializableBitmap(Bitmap bitmap) {
this.bitmap = bitmap;
}
public Bitmap getImage() {
return bitmap;
}
// Converts the Bitmap into a byte array for serialization
private void writeObject(java.io.ObjectOutputStream out) throws IOException {
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 0, byteStream);
byte bitmapBytes[] = byteStream.toByteArray();
out.write(bitmapBytes, 0, bitmapBytes.length);
}
// Deserializes a byte array representing the Bitmap and decodes it
private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException {
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
int b;
while((b = in.read()) != -1)
byteStream.write(b);
byte bitmapBytes[] = byteStream.toByteArray();
bitmap = BitmapFactory.decodeByteArray(bitmapBytes, 0, bitmapBytes.length);
}
}
我从这里得到了代码:Link
我也尝试让每个课程都可以上课,但由于所包含的内容,这个课程也没有成功。
我真的很想看到我的具体问题的一些帮助。
谢谢:)
编辑:我忘了提到如果我像这样启动我的应用程序,方法startActivity(intent)只是关闭应用程序。不会抛出堆栈跟踪或错误。