我很久以前发布了这个问题
答案很好但很难做到,我必须改变我的大部分应用逻辑。
所以我搜索了另一个解决方案,我找到了这个: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
CachedContent类:
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;
}
}