Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I backup SharedPreferences to SD card?

I saw in a lot of places that it's a problem to copy the SharedPreferences file to the sd card because every manufacturer place it somewhere else.

I want to backup on the sd card no matter where is the file located. Is there any way to do this?

like image 954
Elad92 Avatar asked Jun 02 '12 17:06

Elad92


People also ask

Where is shared preference stored?

Android Shared Preferences Overview Android stores Shared Preferences settings as XML file in shared_prefs folder under DATA/data/{application package} directory. The DATA folder can be obtained by calling Environment.

How save and retrieve data using SharedPreferences in Android?

Shared Preferences allow you to save and retrieve data in the form of key,value pair. In order to use shared preferences, you have to call a method getSharedPreferences() that returns a SharedPreference instance pointing to the file that contains the values of preferences.

Can we store large amount of data in SharedPreferences?

SharedPreferences are not intended to store a lot of data, there is no limit per se (since it is an xml file), but for larger sets of data, I would suggest using Room (or SQLite for the older projects). There is also another reason why storing in a database makes more sense.

How much data can we save in SharedPreferences?

2 Answers. Show activity on this post. There's no hard limit. The main reason it is not recommended to use SharedPreferences in place of the database is mainly the performance -> shared preferences data is keept in ordinary flat XML file which lacks all the features SQLite offers.


1 Answers

The SharedPreferences interface contains a method called getAll() which returns a map with the key-value pairs. So instead of copying the file itself, I just serialize the map that being returned from this method and then retrieve it back afterwards.

Some code:

private boolean saveSharedPreferencesToFile(File dst) {
    boolean res = false;
    ObjectOutputStream output = null;
    try {
        output = new ObjectOutputStream(new FileOutputStream(dst));
        SharedPreferences pref = 
                            getSharedPreferences(prefName, MODE_PRIVATE);
        output.writeObject(pref.getAll());

        res = true;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }finally {
        try {
            if (output != null) {
                output.flush();
                output.close();
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    return res;
}

@SuppressWarnings({ "unchecked" })
private boolean loadSharedPreferencesFromFile(File src) {
    boolean res = false;
    ObjectInputStream input = null;
    try {
        input = new ObjectInputStream(new FileInputStream(src));
            Editor prefEdit = getSharedPreferences(prefName, MODE_PRIVATE).edit();
            prefEdit.clear();
            Map<String, ?> entries = (Map<String, ?>) input.readObject();
            for (Entry<String, ?> entry : entries.entrySet()) {
                Object v = entry.getValue();
                String key = entry.getKey();

                if (v instanceof Boolean)
                    prefEdit.putBoolean(key, ((Boolean) v).booleanValue());
                else if (v instanceof Float)
                    prefEdit.putFloat(key, ((Float) v).floatValue());
                else if (v instanceof Integer)
                    prefEdit.putInt(key, ((Integer) v).intValue());
                else if (v instanceof Long)
                    prefEdit.putLong(key, ((Long) v).longValue());
                else if (v instanceof String)
                    prefEdit.putString(key, ((String) v));
            }
            prefEdit.commit();
        res = true;         
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }finally {
        try {
            if (input != null) {
                input.close();
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    return res;
}

I hope that I helped someone, and if something here is wrong please tell me.

Elad

like image 181
Elad92 Avatar answered Sep 20 '22 01:09

Elad92