Copy the shared preferences XML file from /data on Samsung device failed

前端 未结 6 760
粉色の甜心
粉色の甜心 2020-12-09 20:46

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 /da

6条回答
  •  时光说笑
    2020-12-09 21:03

    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//shared_prefs/package.name_preferences.xml and if it exists use it, otherwise fall back to either CommonsWare's suggestion of new File(getFilesDir(), "../shared_prefs") or just /data/data//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/databases file 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//(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//shared_prefs

    That's the real shared_prefs directory.

    However on the Galaxy Tab on Froyo, it's weird. Generally you have: /data/data//(lib|shared_prefs|files|databases)

    With no /dbdata/databases/ directory, but it seems the system apps do have: /dbdata/databases//yourdatabase.db

    And added bonus is that /dbdata/databases/ is not removed when your app is uninstalled. Good luck using SharedPreferences if the user ever reinstalls your app!

提交回复
热议问题