Note that there are some explanatory texts on larger screens.

plurals
  1. POExternal Storage in Android
    primarykey
    data
    text
    <p>I am trying to save an ArrayList to external storage in an Android application, and it seems to fail everytime, but I'm not sure why. I have followed the online tutorials about using external storage and I can't see anything different to the code I have. Here is my read function:</p> <pre><code>public static ArrayList&lt;MapRoute&gt; readFile(Context context, String filename) { ObjectInputStream ois = null; ArrayList&lt;MapRoute&gt; result = null; if (!externalStorageAvailable || !externalStorageWritable) { Log.d("READ FAILED", Environment.getExternalStorageState()); return null; } try { File file = new File(context.getExternalFilesDir(null), filename); FileInputStream fis = new FileInputStream(file); ois = new ObjectInputStream(fis); result = (ArrayList&lt;MapRoute&gt;)ois.readObject(); ois.close(); } catch (Exception e) { Log.d("FAILED", "READ FAILED"); } finally { try { if (ois != null) { ois.close(); } } catch (IOException e) { Log.d("ENDREAD", "FAIL"); } } return result; } </code></pre> <p>And write:</p> <pre><code>public static boolean saveFile(Context context, ArrayList&lt;MapRoute&gt; routes, String filename) { if (!externalStorageAvailable &amp;&amp; !externalStorageWritable) { Log.d("FAILED", "FAIL"); return false; } File file = new File(context.getExternalFilesDir(null), filename); ObjectOutputStream oos = null; boolean success = false; try { OutputStream os = new FileOutputStream(file); oos = new ObjectOutputStream(os); oos.writeObject(routes); success = true; } catch (IOException e) { Log.d("OOS", "FAILED"); } finally { try { if (oos != null) { oos.close(); } } catch (IOException e) { Log.d("OOS", "FAILED2"); } } return success; } </code></pre> <p>In my logcat message the message FAILED READ FAILED from the try block inside the readFile function is outputted, and Im not sure if it is something to do with the way I'm trying to read the array list.</p> <p>Any help on this would be greatly appreciated.</p> <p>EDIT: Here is the stack trace</p> <pre><code> 03-13 22:20:33.690: W/System.err(25353): java.io.WriteAbortedException: Read an exception; java.io.NotSerializableException: mark.es3.mapRoute.MapRoute 03-13 22:20:33.690: W/System.err(25353): at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:933) 03-13 22:20:33.690: W/System.err(25353): at java.io.ObjectInputStream.readObject(ObjectInputStream.java:2262) 03-13 22:20:33.690: W/System.err(25353): at java.io.ObjectInputStream.readObject(ObjectInputStream.java:2217) 03-13 22:20:33.690: W/System.err(25353): at java.util.ArrayList.readObject(ArrayList.java:665) 03-13 22:20:33.690: W/System.err(25353): at java.lang.reflect.Method.invokeNative(Native Method) 03-13 22:20:33.690: W/System.err(25353): at java.lang.reflect.Method.invoke(Method.java:507) 03-13 22:20:33.690: W/System.err(25353): at java.io.ObjectInputStream.readObjectForClass(ObjectInputStream.java:1520) 03-13 22:20:33.690: W/System.err(25353): at java.io.ObjectInputStream.readHierarchy(ObjectInputStream.java:1443) 03-13 22:20:33.695: W/System.err(25353): at java.io.ObjectInputStream.readNewObject(ObjectInputStream.java:2112) 03-13 22:20:33.695: W/System.err(25353): at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:916) 03-13 22:20:33.695: W/System.err(25353): at java.io.ObjectInputStream.readObject(ObjectInputStream.java:2262) 03-13 22:20:33.695: W/System.err(25353): at java.io.ObjectInputStream.readObject(ObjectInputStream.java:2217) 03-13 22:20:33.695: W/System.err(25353): at mark.es3.activities.CreateSessionActivity$FavouriteRoutesTab.readFile(CreateSessionActivity.java:245) 03-13 22:20:33.695: W/System.err(25353): at mark.es3.activities.CreateSessionActivity$FavouriteRoutesTab.onCreate(CreateSessionActivity.java:148) 03-13 22:20:33.695: W/System.err(25353): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 03-13 22:20:33.695: W/System.err(25353): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615) 03-13 22:20:33.695: W/System.err(25353): at android.app.ActivityThread.startActivityNow(ActivityThread.java:1491) 03-13 22:20:33.695: W/System.err(25353): at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127) 03-13 22:20:33.700: W/System.err(25353): at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339) 03-13 22:20:33.700: W/System.err(25353): at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:691) 03-13 22:20:33.700: W/System.err(25353): at android.widget.TabHost.setCurrentTab(TabHost.java:341) 03-13 22:20:33.700: W/System.err(25353): at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:140) 03-13 22:20:33.700: W/System.err(25353): at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:456) 03-13 22:20:33.700: W/System.err(25353): at android.view.View.performClick(View.java:2538) 03-13 22:20:33.700: W/System.err(25353): at android.view.View$PerformClick.run(View.java:9152) 03-13 22:20:33.700: W/System.err(25353): at android.os.Handler.handleCallback(Handler.java:587) 03-13 22:20:33.700: W/System.err(25353): at android.os.Handler.dispatchMessage(Handler.java:92) 03-13 22:20:33.700: W/System.err(25353): at android.os.Looper.loop(Looper.java:123) 03-13 22:20:33.720: W/System.err(25353): at android.app.ActivityThread.main(ActivityThread.java:3691) 03-13 22:20:33.720: W/System.err(25353): at java.lang.reflect.Method.invokeNative(Native Method) 03-13 22:20:33.720: W/System.err(25353): at java.lang.reflect.Method.invoke(Method.java:507) 03-13 22:20:33.720: W/System.err(25353): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847) 03-13 22:20:33.720: W/System.err(25353): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605) 03-13 22:20:33.720: W/System.err(25353): at dalvik.system.NativeStart.main(Native Method) 03-13 22:20:33.720: W/System.err(25353): Caused by: java.io.NotSerializableException: mark.es3.mapRoute.MapRoute 03-13 22:20:33.720: W/System.err(25353): at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1535) 03-13 22:20:33.720: W/System.err(25353): at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1847) 03-13 22:20:33.720: W/System.err(25353): at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1689) 03-13 22:20:33.725: W/System.err(25353): at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1653) 03-13 22:20:33.725: W/System.err(25353): at java.util.ArrayList.writeObject(ArrayList.java:651) 03-13 22:20:33.725: W/System.err(25353): at java.lang.reflect.Method.invokeNative(Native Method) 03-13 22:20:33.725: W/System.err(25353): at java.lang.reflect.Method.invoke(Method.java:507) 03-13 22:20:33.725: W/System.err(25353): at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1219) 03-13 22:20:33.725: W/System.err(25353): at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1575) 03-13 22:20:33.725: W/System.err(25353): at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1847) 03-13 22:20:33.725: W/System.err(25353): at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1689) 03-13 22:20:33.725: W/System.err(25353): at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1653) 03-13 22:20:33.725: W/System.err(25353): at mark.es3.activities.CreateSessionActivity$FavouriteRoutesTab.saveFile(CreateSessionActivity.java:224) 03-13 22:20:33.725: W/System.err(25353): at mark.es3.activities.CreateSessionActivity$FavouriteRoutesTab.saveRoute(CreateSessionActivity.java:207) 03-13 22:20:33.730: W/System.err(25353): at mark.es3.activities.PlotRouteActivity.onHandleActionBarItemClick(PlotRouteActivity.java:94) 03-13 22:20:33.730: W/System.err(25353): at greendroid.app.GDMapActivity$1.onActionBarItemClicked(GDMapActivity.java:247) 03-13 22:20:33.730: W/System.err(25353): at greendroid.widget.ActionBar$1.onClick(ActionBar.java:396) 03-13 22:20:33.730: W/System.err(25353): ... 11 more </code></pre> <p>My MapRoute class does implement Serializable so im not sure what that means.</p>
    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.
 

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