Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Viewing SharedPreferences file?

Tags:

For debugging purposes I need to access the shared preferences file of my application. As far as I know I should find this file in /data/... but I can't access the /data folder through to missing permissions. Is this normal? Any way to still access the file? (except maybe raeding it from inside the application?) The phone is not rooted and I also don't want to root it. Thanks for any hint!

like image 227
stefan.at.wpf Avatar asked May 10 '11 14:05

stefan.at.wpf


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 access SharedPreferences from another activity?

You can create a new shared preference file or access an existing one by calling one of these methods: getSharedPreferences() — Use this if you need multiple shared preference files identified by name, which you specify with the first parameter. You can call this from any Context in your app.

What is getDefaultSharedPreferences?

getDefaultSharedPreferences() is used to get the shared preferences that work in accordance with Android's overall preference framework. getDefaultSharedPreferences() is better to use as it gives the SharedPreferences object that works with a PreferenceActivity by default.


2 Answers

I have ran into this issue in the past (not having root permission on the file system but needing access to the applications data folder). If you don't have a rooted device or a developer device such as the ADP1 then you can try running your application on the emulator and then accessing the files from the "File Explorer" in eclipse or DDMS.

EDIT #1: Try using the getAll function of sharedPreferences and saving that to a file, I will see if I can throw together a sample.

EDIT #2: Example Code, created from random samples around the net, probably not the best way to do it, but I tested it and it does work. It writes a file to the root of your sdcard. Make sure you have

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

set in your manifest

private void saveSharedPreferences()
{
    // create some junk data to populate the shared preferences
    SharedPreferences prefs = getSharedPreferences("MyPrefs", MODE_PRIVATE);
    SharedPreferences.Editor prefEdit = prefs.edit();
    prefEdit.putBoolean("SomeBooleanValue_True", true);
    prefEdit.putInt("SomeIntValue_100", 100);
    prefEdit.putFloat("SomeFloatValue_1.11", 1.11f);
    prefEdit.putString("SomeStringValue_Unicorns", "Unicorns");
    prefEdit.commit();

    // BEGIN EXAMPLE
    File myPath = new File(Environment.getExternalStorageDirectory().toString());
    File myFile = new File(myPath, "MySharedPreferences");

    try
    {
        FileWriter fw = new FileWriter(myFile);
        PrintWriter pw = new PrintWriter(fw);

        Map<String,?> prefsMap = prefs.getAll();

        for(Map.Entry<String,?> entry : prefsMap.entrySet())
        {
            pw.println(entry.getKey() + ": " + entry.getValue().toString());            
        }

        pw.close();
        fw.close();
    }
    catch (Exception e)
    {
        // what a terrible failure...
        Log.wtf(getClass().getName(), e.toString());
    }
}

Sources One Two Three

like image 52
snctln Avatar answered Sep 17 '22 12:09

snctln


On an unrooted phone there is unfortunately no good way to access the /data folder. You might try creating the files with MODE_WORLD_READABLE like so:

SharedPreferences myPrefs = this.getSharedPreferences("prefs", MODE_WORLD_READABLE);

and then try using adb pull to fetch the file to the desktop.

adb pull /data/data/<packagename>/shared_prefs/prefs.xml

but your mileage may vary.

like image 29
Femi Avatar answered Sep 19 '22 12:09

Femi