问题序列化Drawable

我有一个有三个字段的单个对象:两个字符串和一个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 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(); } } } 

当drawable未初始化时,一切正常。 在此先感谢您的帮助。

 java.io.NotSerializableException: android.graphics.drawable.BitmapDrawable 

此消息似乎非常清楚 – photo字段中的特定可绘制实例是BitmapDrawable ,它不是为了序列化而设计的。 如果不处理非序列化字段,则无法序列化您的类。

如果您可以确保您的类始终具有BitmapDrawable或Bitmap ,您可以看到此代码以获取如何处理Bitmap字段的示例:

android如何保存位图 – buggy代码

你无法序列化。

简单地说,如果BitmapDrawable不是Serializable,那么你就无法序列化它。 通常这样的事情是不可序列化的,因为它们坚持引用非纯数据的东西。 像绘图表面的上下文或句柄。