0

I know there are a few threads close to this topic, but I haven't seen answers that fully address what I'm looking for here -- I need to save an ArrayList of Location objects that were generated by Google Play Location Services. Location objects are Parcelable but not Serializable. I attempted to save them via the traditional serializable route, which of course failed... see code below.

    public static void saveArrayListLocations(Context context, ArrayList<Location> dataArrayList) {
    File folder = context.getFilesDir();
    File file = new File(folder, FILE_NAME);

    try {
        file.createNewFile();

        FileOutputStream fos = context.openFileOutput(FILE_NAME, Context.MODE_PRIVATE);
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(dataArrayList);

        oos.close();
        fos.close();

    } catch (FileNotFoundException f) {
        System.out.println("File not found exception while saving");
    } catch (IOException i) {
        System.out.println("IO exception while saving");
    }
}

An IOException is always thrown.

So, questions: Is there a way to serialize Location objects? Or should I be looking into using their Parcelable feature somehow? I am somewhat new at this, and I was wondering if using a Map or a Bundle to package the data would work somehow?

  • 1
    See here: https://stackoverflow.com/questions/13895867/why-does-writeobject-throw-java-io-notserializableexception-and-how-do-i-fix-it, this may help. – JJJones_3860 Aug 16 '21 at 14:35

0 Answers0