Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot save ArrayList of Uri in a file: NotSerializableException

I must save an ArrayList of Ury so I use the code

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(); 
            }
        }

    }

This code structure works fine for custom object that implements serializable but with Uri objects returns this Exception

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

I suppose that this happen because Uri doesn't implements Serializable....

How could I fix this problem? Must I create a custom Uri Object?

like image 367
AndreaF Avatar asked Dec 20 '22 14:12

AndreaF


2 Answers

As you already noticed, Uri does not implement Serializable, see the reference android.net.Uri. How about storing your uris as Strings when writing your file, and converting them back to uris using Uri.parse() when reading the file?

like image 56
darksaga Avatar answered May 09 '23 22:05

darksaga


when saving

String str = myUri.toString();

and when loading

Uri myUri = Uri.parse(str);
like image 29
Simon K. Gerges Avatar answered May 09 '23 22:05

Simon K. Gerges