javaandroidserializationfileoutputstreamobjectoutputstream

Persist Map <Integer, Integer> in android


I tried this weekend with different approaches to store data of a map <Integer, Integer> in android. Unfortunately, this has not worked as desired.

Corresponding code section:

                    if (getCounter() > 19){

                    try
                    {
                        File folder = new File(this.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "hashmap");

                        if (!folder.exists())
                        {
                            folder.mkdirs();
                        }

                        File ganiuSave = new File(folder, "mapDEGR");
                        OutputStream mapSave = new FileOutputStream(ganiuSave);

                        ObjectOutputStream oos = new ObjectOutputStream(mapSave);
                        oos.writeObject(mapDEGR);
                        oos.close();
                        mapSave.close();
                    }
                    catch(IOException ioe)
                    {
                        ioe.printStackTrace();
                    }}

I could not locate the corresponding file on the smartphone. The app was previously transferred to the smartphone using android debug bridge. The map contains values, so this should not be a reason why no saving takes place. All classes involved implement Serializable and no exception got thrown.

Anyone any ideas?

Tell me if you need more surrounding code to evaluate the issue. Code segment is located inside a switch statement.


Solution

  • To locate your file you can log your file's absolute location like this:

    Log.v(LOG_TAG, "File Location: " + ganiuSave.getAbsolutePath());
    

    Once you get the exact location, you can reach the file by using ADB or Android Studio's "Device File Explorer". When I tried with your code I'm getting:

    On the emulator api 17;

    /mnt/sdcard/Android/data/com.example.yourapp/files/Download/hashmap/mapDEGR
    

    on a physical device api 24;

    /storage/emulated/0/Android/data/com.example.yourapp/files/Download/hashmap/mapDEGR