Note that there are some explanatory texts on larger screens.

plurals
  1. PONot Serializable exception Android
    primarykey
    data
    text
    <p>i´ve posted this question short time ago</p> <p><a href="https://stackoverflow.com/questions/17042130/save-array-of-drawable-to-file-android">Question</a></p> <p>The answer was good but its really hard to do and i have to change most of my app logic.</p> <p>so i searched for another solution, i found this: <a href="https://stackoverflow.com/questions/9219023/best-way-to-serialize-deserialize-an-image-in-android">Best way to Serialize / Deserialize an image in Android</a></p> <p>So using that class i can serialize my arraylist of images without so much pain.</p> <p>I´ve made needed changes to implement it, so my item class is at follows.</p> <pre><code>public class SeccionItem implements Serializable{ String name; String text; ArrayList&lt;SerializableImage&gt; img; SeccionItem() { img = new ArrayList&lt;SerializableImage&gt;(); } } </code></pre> <p>But im still getting NotSerializableException for SeccionItem and i really dont know why.</p> <p>I dont want to save each image on separate files from the input stream because i have many images related to a object and i dont want to have so much files for one part of my app.</p> <p>code of serializableImage</p> <pre><code>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); } } </code></pre> <p>Logcat just says WriteAbortedException caused by NotSerializableException on my call to oos.writeObject(data);</p> <p>Complete stack trace</p> <pre><code>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 </code></pre> <p>CachedContent class:</p> <pre><code>public class CachedContent { public static void saveData(String filename,Context context,ArrayList&lt;?&gt; 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&lt;?&gt; getData(String filename,Context context) { try{ File path = new File(context.getFilesDir(),filename); ObjectInputStream ois = new ObjectInputStream(new FileInputStream(path)); ArrayList&lt;?&gt; data = (ArrayList&lt;?&gt;)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 &amp;&amp; netInfo.isConnected()){ return true; } return false; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload