There’s an exporting feature in my application. It’s just a copy operation since all my settings are store in shared preference.
I just copy the xml file from /data/data/package.name/shared_prefs/settings.xml to SD card. It works fine on my HTC desire. However, it might not work on Samsung devices, and i got the following error while I try to copy the file.
I/System.out( 3166): /data/data/package.name/shared_prefs/settings.xml (No such file or directory) in the directory.
Anyone know how to fix it, or is there another simple way to store the shared preference ?
Thanks.
CommonsWare’s suggestion would a be clever hack, but unfortunately it won’t work.
Samsung does not always put the shared_prefs directory in the same parent directory as the
getFilesDir().I’d recommend testing for the existence of (hardcode it, except for package name):
/dbdata/databases/<package_name>/shared_prefs/package.name_preferences.xmland if it exists use it, otherwise fall back to either CommonsWare’s suggestion ofnew File(getFilesDir(), "../shared_prefs")or just/data/data/<package_name>/shared_prefs/package.name_preferences.xml.A warning though that this method could potentially have problems if a user switched from a Samsung rom to a custom rom without wiping, as the
/dbdata/databasesfile might be unused but still exist.More details
On some Samsung devices, such as the Galaxy S series running froyo, the setup is this:
/data/data/<package_name>/(lib|files|databases)Sometimes there’s a shared_prefs there too, but it’s just Samsung’s attempt to confuse you! Don’t trust it! (I think it can happen as a left over from a 2.1 upgrade to 2.2, but it might be a left over from users switching roms. I don’t really know, I just have both included in my app’s bug report interface and sometimes see both files).
And:
/dbdata/databases/<package_name>/shared_prefsThat’s the real shared_prefs directory.
However on the Galaxy Tab on Froyo, it’s weird. Generally you have:
/data/data/<package_name>/(lib|shared_prefs|files|databases)With no
/dbdata/databases/<package_name>directory, but it seems the system apps do have:/dbdata/databases/<package_name>/yourdatabase.dbAnd added bonus is that
/dbdata/databases/<package_name>is not removed when your app is uninstalled. Good luck using SharedPreferences if the user ever reinstalls your app!