Я пытаюсь сохранить ArrayList во внешнем хранилище в приложении Android, и, похоже, он каждый раз дает сбой, но я не уверен, почему.Я следовал онлайн-учебникам по использованию внешнего хранилища, и я не вижу ничего отличного от кода, который у меня есть.Вот моя функция чтения:
public static ArrayList<MapRoute> readFile(Context context, String filename) {
ObjectInputStream ois = null;
ArrayList<MapRoute> 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<MapRoute>)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;
}
И запись:
public static boolean saveFile(Context context, ArrayList<MapRoute> routes, String filename) {
if (!externalStorageAvailable && !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;
}
В моем сообщении logcat выдается сообщение FAILED READ FAILED из блока try внутри функции readFile, а я - нетуверен, что это как-то связано с тем, как я пытаюсь прочитать список массивов.
Любая помощь по этому вопросу будет принята с благодарностью.
РЕДАКТИРОВАТЬ: Вот трассировка стека
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
Мой класс MapRoute реализует Serializable, поэтому я не уверен, что это значит.