我必须保存 Ury 的 ArrayList,所以我使用代码
public static void saveUriList(ArrayList<Uri> myUriList) {
if (storageAvailable()) {
try {
FileOutputStream uriListSaved = new FileOutputStream(
baseDir + File.separator + filename);
ObjectOutputStream uriList = new ObjectOutputStream(
uriListSaved);
uriList.writeObject(myUriList);
uriList.close();
} catch (Exception exc) {
exc.printStackTrace();
}
}
}
此代码结构适用于实现可序列化但使用 Uri 对象返回此异常的自定义对象
10-18 21:06:40.169: W/System.err(11957): java.io.WriteAbortedException: Read an exception; java.io.NotSerializableException: android.net.Uri$StringUri
10-18 21:06:40.169: W/System.err(11957): at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:933)
10-18 21:06:40.169: W/System.err(11957): at java.io.ObjectInputStream.readObject(ObjectInputStream.java:2262)
10-18 21:06:40.169: W/System.err(11957): at java.io.ObjectInputStream.readObject(ObjectInputStream.java:2217)
10-18 21:06:40.169: W/System.err(11957): at java.util.ArrayList.readObject(ArrayList.java:665)
10-18 21:06:40.169: W/System.err(11957): at java.lang.reflect.Method.invokeNative(Native Method)
10-18 21:06:40.169: W/System.err(11957): at java.lang.reflect.Method.invoke(Method.java:507)
10-18 21:06:40.169: W/System.err(11957): at java.io.ObjectInputStream.readObjectForClass(ObjectInputStream.java:1520)
10-18 21:06:40.169: W/System.err(11957): at java.io.ObjectInputStream.readHierarchy(ObjectInputStream.java:1443)
10-18 21:06:40.169: W/System.err(11957): at java.io.ObjectInputStream.readNewObject(ObjectInputStream.java:2112)
10-18 21:06:40.169: W/System.err(11957): at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:916)
10-18 21:06:40.169: W/System.err(11957): at java.io.ObjectInputStream.readObject(ObjectInputStream.java:2262)
10-18 21:06:40.169: W/System.err(11957): at java.io.ObjectInputStream.readObject(ObjectInputStream.java:2217)
10-18 21:06:40.169: W/System.err(11957): at it.myApp.testApp.utility.FileArchiveManager.loadUriList(FileArchiveManager.java:158)
10-18 21:06:40.169: W/System.err(11957): at it.myApp.testApp.MyClass.onCreate(MyClass.java:107)
10-18 21:06:40.169: W/System.err(11957): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-18 21:06:40.169: W/System.err(11957): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
10-18 21:06:40.169: W/System.err(11957): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
10-18 21:06:40.169: W/System.err(11957): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
10-18 21:06:40.169: W/System.err(11957): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
10-18 21:06:40.169: W/System.err(11957): at android.os.Handler.dispatchMessage(Handler.java:99)
10-18 21:06:40.169: W/System.err(11957): at android.os.Looper.loop(Looper.java:130)
10-18 21:06:40.169: W/System.err(11957): at android.app.ActivityThread.main(ActivityThread.java:3687)
10-18 21:06:40.179: W/System.err(11957): at java.lang.reflect.Method.invokeNative(Native Method)
10-18 21:06:40.179: W/System.err(11957): at java.lang.reflect.Method.invoke(Method.java:507)
10-18 21:06:40.179: W/System.err(11957): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
10-18 21:06:40.179: W/System.err(11957): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
10-18 21:06:40.179: W/System.err(11957): at dalvik.system.NativeStart.main(Native Method)
10-18 21:06:40.179: W/System.err(11957): Caused by: java.io.NotSerializableException: android.net.Uri$StringUri
10-18 21:06:40.179: W/System.err(11957): at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1535)
10-18 21:06:40.179: W/System.err(11957): at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1847)
10-18 21:06:40.179: W/System.err(11957): at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1689)
10-18 21:06:40.179: W/System.err(11957): at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1653)
10-18 21:06:40.179: W/System.err(11957): at java.util.ArrayList.writeObject(ArrayList.java:651)
10-18 21:06:40.179: W/System.err(11957): at java.lang.reflect.Method.invokeNative(Native Method)
10-18 21:06:40.179: W/System.err(11957): at java.lang.reflect.Method.invoke(Method.java:507)
10-18 21:06:40.179: W/System.err(11957): at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1219)
10-18 21:06:40.179: W/System.err(11957): at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1575)
10-18 21:06:40.179: W/System.err(11957): at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1847)
10-18 21:06:40.179: W/System.err(11957): at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1689)
10-18 21:06:40.179: W/System.err(11957): at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1653)
10-18 21:06:40.179: W/System.err(11957): at it.myApp.testApp.utility.FileArchiveManager.salvaListaPathImmagini(FileArchiveManager.java:127)
10-18 21:06:40.179: W/System.err(11957): at it.myApp.testApp.LoadImage.onActivityResult(LoadImage.java:120)
10-18 21:06:40.179: W/System.err(11957): at android.app.Activity.dispatchActivityResult(Activity.java:3908)
10-18 21:06:40.179: W/System.err(11957): at android.app.ActivityThread.deliverResults(ActivityThread.java:2532)
10-18 21:06:40.179: W/System.err(11957): at android.app.ActivityThread.handleSendResult(ActivityThread.java:2578)
10-18 21:06:40.179: W/System.err(11957): at android.app.ActivityThread.access$2000(ActivityThread.java:117)
10-18 21:06:40.179: W/System.err(11957): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:965)
10-18 21:06:40.189: W/System.err(11957): ... 8 more
我想这是因为 Uri 没有实现 Serializable....
我该如何解决这个问题?我必须创建自定义 Uri 对象吗?