Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

android

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.

like image 232
dong221 Avatar asked Apr 03 '11 17:04

dong221


People also ask

Where is shared preference file stored android?

Android stores Shared Preferences settings as XML file in shared_prefs folder under DATA/data/{application package} directory. The DATA folder can be obtained by calling Environment. getDataDirectory() .

How can I see shared preferences in Android?

Open the device monitor by clicking it. Then you need to select the File Explorer tab in the device monitor. Find the data folder and find another data folder inside it. It will contain a folder having the name of your application package and there will be the desired SharedPreferences.

Where is shared preference file stored flutter?

SharedPreferences is used for storing data key-value pair in the Android and iOS. SharedPreferences in flutter uses NSUserDefaults on iOS and SharedPreferences on Android, providing a persistent store for simple data.


2 Answers

Never never never never never never never never never hardwire paths.

Unfortunately, there's no getSharedPreferenceDir() anywhere that I can think of. The best solution I can think of will be:

new File(getFilesDir(), "../shared_prefs")

This way if a device manufacturer elects to change partition names, you are covered.

Try this and see if it helps.

like image 136
CommonsWare Avatar answered Sep 20 '22 12:09

CommonsWare


Try using

context.getFilesDir().getParentFile().getAbsolutePath()
like image 22
Xiaoxi Zhang Avatar answered Sep 21 '22 12:09

Xiaoxi Zhang