我有一个具有三个字段的单一对象:两个字符串和一个Drawable
public class MyObject implements Serializable {
private static final long serialVersionUID = 1L;
public String name;
public String lastName;
public Drawable photo;
public MyObject() {
}
public MyObject(String name, String lastName, Drawable photo) {
this.name = name;
this.lastName = lastName;
this.photo = photo;
}
}
我想要做的是将ArrayList
这些对象中的一个保存到文件中,但我不断得到一个NotSerializableException
02-02 23:06:10.825: WARN/System.err(13891): java.io.NotSerializableException: android.graphics.drawable.BitmapDrawable
我用来存储文件的代码:
public static void saveArrayList(ArrayList<MyObject> arrayList, Context context) {
final File file = new File(context.getCacheDir(), FILE_NAME);
FileOutputStream outputStream = null;
ObjectOutputStream objectOutputStream = null;
try {
outputStream = new FileOutputStream(file);
objectOutputStream = new ObjectOutputStream(outputStream);
objectOutputStream.writeObject(arrayList);
}
catch(Exception e) {
e.printStackTrace();
}
finally {
try {
if(objectOutputStream != null) {
objectOutputStream.close();
}
if(outputStream != null) {
outputStream.close();
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
当可绘制对象未初始化时,一切正常。提前感谢您的帮助。