从复制的共享preferences XML文件/三星设备上的数据失败文件、数据、设备、preferences

2023-09-13 00:50:19 作者:一直奔跑就不會輸

有一个在我的应用程序中的导出功能。这只是一个复制操作,因为我所有的设置都存放在共享preference。

There's an exporting feature in my application. It's just a copy operation since all my settings are store in shared preference.

我刚刚从/data/data/package.name/shared_$p$pfs/settings.xml复制XML文件到SD卡。它工作正常在我的HTC渴望。但是,它可能无法在三星设备的工作,我得到了以下错误,而我试图复制该文件。

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.

任何人都知道如何解决它,或有存储的共享preference另一种简单的方法是什么?

Anyone know how to fix it, or is there another simple way to store the shared preference ?

感谢。

推荐答案

CommonsWare的建议是一个聪明的黑客,但不幸的是它不会工作。

CommonsWare's suggestion would a be clever hack, but unfortunately it won't work.

三星并不总是把shared_ preFS目录中相同的父目录中的 getFilesDir()

Samsung does not always put the shared_prefs directory in the same parent directory as the getFilesDir().

我推荐的(硬code是,除了包名)的存在测试: /dbdata/databases/<package_name>/shared_$p$pfs/package.name_$p$pferences.xml 键,如果它存在使用它,否则回落要么的CommonsWare的建议新建文件(getFilesDir(),../ shared_ preFS)或只是 /数据/数据​​/&LT;程序包&GT ;。/shared_$p$pfs/package.name_$p$pferences.xml

I'd recommend testing for the existence of (hardcode it, except for package name): /dbdata/databases/<package_name>/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/<package_name>/shared_prefs/package.name_preferences.xml.

一个警告,但这种方法可能有问题,如果用户从三星ROM切换到自定义ROM无需擦拭,因为 / dbdata /数据库文件可能是未使用的,但仍然存在。

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.

在一些三星设备,如银河S系列运行Froyo的,设置是这样的:

On some Samsung devices, such as the Galaxy S series running froyo, the setup is this:

有时候,有一个shared_ preFS有太多,但它只是三星的企图迷惑你!不要相信它! (我认为它可以发生,因为一个遗留从2.1升级到2.2,但它可能是一个来自用户的切换ROM中遗留下来的。我真的不知道,我只是两者包含在我的应用程序的错误报告界面有时见这两个文件)。

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).

这是真正的shared_ preFS目录。

That's the real shared_prefs directory.

但在Galaxy Tab的升级Froyo上,它的怪异。一般来说,你有: /数据/数据​​/&LT;程序包&GT; /(LIB | shared_ preFS |文件|数据库)

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

由于没有 / dbdata /数据库/&LT;程序包&GT; 目录,但似乎该系统的应用程序确实有: /dbdata/databases/<package_name>/yourdatabase.db

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

和好处是, / dbdata /数据库/&LT;程序包&GT; 时,您的应用程序卸载不会被删除。祝你好运使用共享preferences如果用户不断重新安装你的应用程序!

And 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!