20

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!

stefan.at.kotlin
  • 15,347
  • 38
  • 147
  • 270

3 Answers3

18

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

Community
  • 1
  • 1
snctln
  • 12,175
  • 6
  • 45
  • 42
  • unfortunately my application involves wlan and wlan is not available on the emulator, isn't it? – stefan.at.kotlin May 10 '11 at 14:43
  • I do not believe wlan is available on the emulator, I will take another look – snctln May 10 '11 at 14:48
  • snctln, i am having the same idea, don't o any work, i will try it, no sample needed ;-) – stefan.at.kotlin May 10 '11 at 14:54
  • ok I have one problem, in Map getAll (), what does the "?" mean? Missing some Java knowledge on this ): – stefan.at.kotlin May 10 '11 at 15:06
  • Ahh, got the example up before i saw your response :) I had to look up the "?" as well, I ended up reading http://www.angelikalanger.com/GenericsFAQ/FAQSections/ParameterizedTypes.html and saw that the "?" is a wildcard that can mean any type. So rather than try to get fancy and save it as a variable I just used "getValue().toString()" on the Map Entry – snctln May 10 '11 at 15:19
  • snctln, thanks for your great help :-) and you were faster than me, so it was still useful for me :-) – stefan.at.kotlin May 10 '11 at 16:11
1

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.

Femi
  • 64,273
  • 8
  • 118
  • 148
  • hmm how do I actually create the file? I used xml to specify my preferences and android handles the storage of it... I just need to retrieve values from it though code. Thanks for any hint and for your reply :-) – stefan.at.kotlin May 10 '11 at 14:42
  • The file is automatically created by the Preferences framework: if you need to access the data in your code you should be able to just use the preferences framework as described at http://developer.android.com/guide/topics/data/data-storage.html#pref – Femi May 10 '11 at 14:49
  • thanks femi, that works but somehow I am using the wrong key or android stores something different than expected... that's why I'd like to look at all values / the file itself ): edit: well there is the getAll() metho, will have a look at it. – stefan.at.kotlin May 10 '11 at 14:53
  • Ah, odd. Wrong values as in *get returns nothing after you've set and used commit()* or *get returns different things than what you've set*? – Femi May 10 '11 at 15:05
  • Actually I don't set anything manually, the android system does. I am just reading the shared preferences and I get values back, but it aren't those they should be. I guess I am mixing two keys, if I could have a look at all saved key-value-pairs, then I could pick the right one. – stefan.at.kotlin May 10 '11 at 15:09
  • Well, shared preferences won't give you any values back if you don't write any values into it? Put up a code sample up? – Femi May 10 '11 at 15:12
  • Well, Android writes them and I read and use them. I have a PreferenceScreen specified in XML like this one here http://developer.android.com/reference/android/preference/PreferenceScreen.html Android then saves the user's selection / input in the shared preferences and I read and use them, but I don't write code to save them. – stefan.at.kotlin May 10 '11 at 17:38
  • Ah, alright: was confused for a second. You probably need to use this syntax to fetch the preferences: `SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());`. Do that in your Activity and you should be good. – Femi May 10 '11 at 17:49
  • thanks femi, my problem is already solved ;-) but the problem was not getting the preference, but finding the right key ;-) – stefan.at.kotlin May 10 '11 at 20:44
  • Ah, alright. Good. The problem existed between the chair and keyboard, eh :)? – Femi May 10 '11 at 21:36
  • Possibly :P [some more chars...] – stefan.at.kotlin May 11 '11 at 12:10
0

You can access and modify SharedPreferences data easily by using stetho. Follow the link provided below to add it to your android project and view data.

http://facebook.github.io/stetho/

Avik Chowdhury
  • 171
  • 1
  • 1
  • 7