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?
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?
The
SharedPreferences
interface contains a method calledgetAll()
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:
I hope that I helped someone, and if something here is wrong please tell me.
Elad
An alternative to using ObjectOutputStream/ObjectInputStream is to add the XmlUtils.java and FastXmlSerializer.java files from the Android source to your project, and then use XmlUtils.writeMapXml() and XmlUtils.readMapXml():
.....
.....
The storage format will then be the same XML as is used to store the SharedPreferences.