我不久前发布了这个问题
答案很好,但真的很难做到,我必须改变我的大部分应用程序逻辑。
所以我搜索了另一个解决方案,我发现了这个:Best way to Serialize / Deserialize an image in Android
所以使用那个类,我可以序列化我的图像数组列表而不会那么痛苦。
我已经进行了必要的更改来实现它,所以我的项目类如下。
public class SeccionItem implements Serializable{
String name;
String text;
ArrayList<SerializableImage> img;
SeccionItem()
{
img = new ArrayList<SerializableImage>();
}
}
但是我仍然为 SeccionItem 获得 NotSerializableException,我真的不知道为什么。
我不想将每个图像保存在来自输入流的单独文件中,因为我有许多与对象相关的图像,我不想为我的应用程序的一部分保存这么多文件。
serializableImage的代码
public class SerializableImage implements Serializable {
private transient static final int NO_IMAGE = -1;
private transient Bitmap image;
public Bitmap getImage() {
return image;
}
public void setImage(Bitmap image) {
this.image = image;
}
SerializableImage(Bitmap b)
{
image = b;
}
private void writeObject(ObjectOutputStream out) throws IOException {
if (image != null) {
final ByteArrayOutputStream stream = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.PNG, 100, stream);
final byte[] imageByteArray = stream.toByteArray();
out.writeInt(imageByteArray.length);
out.write(imageByteArray);
} else {
out.writeInt(NO_IMAGE);
}
}
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException
{
final int length = in.readInt();
if (length != NO_IMAGE) {
final byte[] imageByteArray = new byte[length];
image = BitmapFactory.decodeByteArray(imageByteArray, 0, length);
}
}
Logcat 只是在我调用 oos.writeObject(data); 时说由 NotSerializableException 引起的 WriteAbortedException;
完整的堆栈跟踪
06-11 13:23:51.429: W/System.err(22409): java.io.WriteAbortedException: Read an exception; java.io.NotSerializableException: com.example.app.SeccionItem
06-11 13:23:51.449: W/System.err(22409): at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:933)
06-11 13:23:51.449: W/System.err(22409): at java.io.ObjectInputStream.readObject(ObjectInputStream.java:2262)
06-11 13:23:51.459: W/System.err(22409): at java.io.ObjectInputStream.readObject(ObjectInputStream.java:2217)
06-11 13:23:51.459: W/System.err(22409): at java.util.ArrayList.readObject(ArrayList.java:665)
06-11 13:23:51.459: W/System.err(22409): at java.lang.reflect.Method.invokeNative(Native Method)
06-11 13:23:51.469: W/System.err(22409): at java.lang.reflect.Method.invoke(Method.java:507)
06-11 13:23:51.469: W/System.err(22409): at java.io.ObjectInputStream.readObjectForClass(ObjectInputStream.java:1520)
06-11 13:23:51.469: W/System.err(22409): at java.io.ObjectInputStream.readHierarchy(ObjectInputStream.java:1443)
06-11 13:23:51.469: W/System.err(22409): at java.io.ObjectInputStream.readNewObject(ObjectInputStream.java:2112)
06-11 13:23:51.469: W/System.err(22409): at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:916)
06-11 13:23:51.469: W/System.err(22409): at java.io.ObjectInputStream.readObject(ObjectInputStream.java:2262)
06-11 13:23:51.479: W/System.err(22409): at java.io.ObjectInputStream.readObject(ObjectInputStream.java:2217)
06-11 13:23:51.479: W/System.err(22409): at com.example.app.CachedContent.getData(CachedContent.java:35)
06-11 13:23:51.479: W/System.err(22409): at com.example.app.SeccionParser.load(SeccionParser.java:46)
06-11 13:23:51.479: W/System.err(22409): at com.example.app.Downloader.run(Downloader.java:28)
06-11 13:23:51.479: W/System.err(22409): Caused by: java.io.NotSerializableException: com.example.app.SeccionItem
06-11 13:23:51.479: W/System.err(22409): at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1535)
06-11 13:23:51.479: W/System.err(22409): at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1847)
06-11 13:23:51.479: W/System.err(22409): at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1689)
06-11 13:23:51.489: W/System.err(22409): at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1653)
06-11 13:23:51.489: W/System.err(22409): at java.util.ArrayList.writeObject(ArrayList.java:651)
06-11 13:23:51.489: W/System.err(22409): at java.lang.reflect.Method.invokeNative(Native Method)
06-11 13:23:51.499: W/System.err(22409): at java.lang.reflect.Method.invoke(Method.java:507)
06-11 13:23:51.499: W/System.err(22409): at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1219)
06-11 13:23:51.499: W/System.err(22409): at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1575)
06-11 13:23:51.499: W/System.err(22409): at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1847)
06-11 13:23:51.499: W/System.err(22409): at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1689)
06-11 13:23:51.499: W/System.err(22409): at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1653)
06-11 13:23:51.499: W/System.err(22409): at com.example.app.CachedContent.saveData(CachedContent.java:22)
06-11 13:23:51.499: W/System.err(22409): at com.example.app.SeccionParser.load(SeccionParser.java:78)
06-11 13:23:51.499: W/System.err(22409): ... 1 more
缓存内容类:
public class CachedContent {
public static void saveData(String filename,Context context,ArrayList<?> data)
{
try{
File path = new File(context.getFilesDir(),filename);
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(path));
oos.writeObject(data);
oos.close();
}catch(Exception ex)
{
ex.printStackTrace();
}
}
public static ArrayList<?> getData(String filename,Context context)
{
try{
File path = new File(context.getFilesDir(),filename);
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(path));
ArrayList<?> data = (ArrayList<?>)ois.readObject();
ois.close();
return data;
}catch(Exception ex)
{
ex.printStackTrace();
return null;
}
}
public static void saveDate(Date date, String filename,Context context)
{
try{
File path = new File(context.getFilesDir(),filename+"_date");
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(path));
oos.writeObject(date);
oos.close();
}catch(Exception ex)
{
ex.printStackTrace();
}
}
public static Date getDate(String filename,Context context)
{
try{
File path = new File(context.getFilesDir(),filename+"_date");
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(path));
Date d =(Date)ois.readObject();
ois.close();
return d;
}catch(Exception ex)
{
ex.printStackTrace();
return null;
}
}
public static boolean hasInternet(Context a){
ConnectivityManager cm = (ConnectivityManager)a.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if(netInfo!=null && netInfo.isConnected()){
return true;
}
return false;
}
}