开发者

Shared Preferences Path not exists on some phone?

开发者 https://www.devze.com 2023-03-03 08:45 出处:网络
The default shared preferences path on Android is /data/data/package_name/shared_prefs/package_name_preferences.xml

The default shared preferences path on Android is

/data/data/package_name/shared_prefs/package_name_preferences.xml

In my app, I have an option to backup this file to SD-card. But on the phone Samsung Galaxy S, it seems there is no path or file like

/data/data/package_name/..

A user reported to me that he is getting an error on backup.

If I check the error, I can see:

/data/data/xxx.xxxx.xxxx/shared_prefs/xxx.xxxx.xxxx_preferences.xml (No such file or directory)

How can this happen?

Here is my code to create the path:

Environment.getDataDirectory().toString() + "/data开发者_如何学Python/xxx.xxxx.xxxx/shared_pref/xxx.xxxx.xxxxx_preferences.xml"


Have you considered reimplementing it using Android Cloud Backup so the backup goes to the cloud instead of the SD card?

http://developer.android.com/guide/topics/data/backup.html


You shouldn't use absolute file paths like that in your app because not all Android installs use the same paths.

Try using: SharedPreferences settings = getSharedPreferences(xxx.xxxx.xxxx_preferences.xml, 0);

That will access your Shared preferences using relative addressing and not absolute addressing and should fix your problem.


I just tried to get path of shared preferences below like this.This is work for me.

File f = getDatabasePath("MyPrefsFile.xml");

if (f != null)
    Log.i("TAG", f.getAbsolutePath());
0

精彩评论

暂无评论...
验证码 换一张
取 消