How can I view the shared preferences file using Android Studio?

前端 未结 14 1021
甜味超标
甜味超标 2020-11-27 11:33

I\'m using shared preferences to store certain values for my app. I would like to see the file where the info is actually stored on my phone. I found many ways to do this on

相关标签:
14条回答
  • 2020-11-27 12:01

    The Device File Explorer that is part of Android Studio 3.x is really good for exploring your preference file(s), cache items or database.

    1. Shared Preferences /data/data//shared_prefs directory

    It looks something like this

    To open The Device File Explorer:

    Click View > Tool Windows > Device File Explorer or click the Device File Explorer button in the tool window bar.

    0 讨论(0)
  • 2020-11-27 12:05

    Single or Multiple Shared Preference Files

    If you have multiple Shared Preference files, then here is a good way to show all of them, but you can just pass in 1 filename, too.

    • loadSharedPrefs("pref_name");

    • loadSharedPrefs("shared_pref1", "shared_pref2", "shared_pref3");

    Choose one of the following to suit your needs...

    Single-Type Values

    public void loadSharedPrefs(String ... prefs) {
    
        // Logging messages left in to view Shared Preferences. I filter out all logs except for ERROR; hence why I am printing error messages.
    
        Log.i("Loading Shared Prefs", "-----------------------------------");
        Log.i("----------------", "---------------------------------------");
    
        for (String pref_name: prefs) {
    
            SharedPreferences preference = getSharedPreferences(pref_name, MODE_PRIVATE);
            for (String key : preference.getAll().keySet()) {
    
                Log.i(String.format("Shared Preference : %s - %s", pref_name, key),
                      preference.getString(key, "error!"));
    
            }
    
            Log.i("----------------", "---------------------------------------");
    
        }
    
        Log.i("Finished Shared Prefs", "----------------------------------");
    
    }
    

    Multiple-Type Values

    public void loadSharedPrefs(String ... prefs) {
    
        // Define default return values. These should not display, but are needed
        final String STRING_ERROR = "error!";
        final Integer INT_ERROR = -1;
        // ...
        final Set<String> SET_ERROR = new HashSet<>(1);
    
        // Add an item to the set
        SET_ERROR.add("Set Error!");
    
        // Loop through the Shared Prefs
        Log.i("Loading Shared Prefs", "-----------------------------------");
        Log.i("------------------", "-------------------------------------");
    
        for (String pref_name: prefs) {
    
            SharedPreferences preference = getSharedPreferences(pref_name, MODE_PRIVATE);
            Map<String, ?> prefMap = preference.getAll();
    
            Object prefObj;
            Object prefValue = null;
    
            for (String key : prefMap.keySet()) {
    
                prefObj = prefMap.get(key);
    
                if (prefObj instanceof String) prefValue = preference.getString(key, STRING_ERROR);
                if (prefObj instanceof Integer) prefValue = preference.getInt(key, INT_ERROR);
                // ...
                if (prefObj instanceof Set) prefValue = preference.getStringSet(key, SET_ERROR);
    
                Log.i(String.format("Shared Preference : %s - %s", pref_name, key),
                      String.valueOf(prefValue));
    
            }
    
            Log.i("------------------", "-------------------------------------");
    
        }
    
        Log.i("Loaded Shared Prefs", "------------------------------------");
    
    }
    
    }
    

    Logcat Output

    My Shared Preference values are all String, but this is the output using either of the 2 methods above...

    I/Loading Shared Prefs﹕ -----------------------------------
    I/------------------﹕ -------------------------------------
    I/Shared Preference : FAVORITE - 135397﹕ Jurassic World
    I/Shared Preference : FAVORITE - 87101﹕ Terminator Genisys
    I/Shared Preference : FAVORITE - 177677﹕ Mission: Impossible – Rogue Nation
    I/------------------﹕ -------------------------------------
    I/Shared Preference : WATCHED - 177677﹕ Mission: Impossible – Rogue Nation
    I/Shared Preference : WATCHED - 157336﹕ Interstellar
    I/Shared Preference : WATCHED - 135397﹕ Jurassic World
    I/Shared Preference : WATCHED - 87101﹕ Terminator Genisys
    I/------------------﹕ -------------------------------------
    I/Shared Preference : WILL_WATCH - 211672﹕ Minions
    I/Shared Preference : WILL_WATCH - 102899﹕ Ant-Man
    I/------------------﹕ -------------------------------------
    I/Loaded Shared Prefs﹕ ------------------------------------
    
    0 讨论(0)
  • 2020-11-27 12:08

    Stetho

    You can use http://facebook.github.io/stetho/ for accessing your shared preferences while your application is in the debug mode. No Root

    features:

    1. view and edit sharedpreferences
    2. view and edit sqLite db
    3. view view heirarchy
    4. monitor http network requests
    5. view stream from the device's screen
    6. and more....

    Basic setup:

    1. in the build.gradle add compile 'com.facebook.stetho:stetho:1.5.0'
    2. in the application's onCreate() add Stetho.initializeWithDefaults(this);
    3. in Chrome on your PC go to the chrome://inspect/

    UPDATE: Flipper

    Flipper is a newer alternative from facebook. It has more features but for the time writing is only available for Mac, slightly harder to configure and lacks data base debugging, while brining up extreamely enhanced layout inspector

    You can also use @Jeffrey suggestion:

    • Open Device File Explorer (Lower Right of screen)
    • Go to data/data/com.yourAppName/shared_prefs
    0 讨论(0)
  • 2020-11-27 12:08

    Android Studio -> Device File Explorer (right bottom corner) -> data -> data -> {package.id} -> shared-prefs

    Note: You need to connect mobile device to android studio and selected application should be in debug mode

    0 讨论(0)
  • 2020-11-27 12:15

    This is an old post, but I though I should put a graphical answer here as the question is about viewing the SharedPreferences.xml using Android Studio. So here it goes.

    Go to the Tools -> Android Device Monitor. 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.xml.

    Select the SharedPreferences.xml file and then pull and save the file in your computer using the button marked at the top-right corner of the image above.

    I've used a device emulator.

    0 讨论(0)
  • 2020-11-27 12:15

    If you're using an emulator you can see the sharedPrefs.xml file on the terminal with this commands:

    • adb root
    • cat /data/data/<project name>/shared_prefs/<xml file>

    after that you can use adb unroot if you dont want to keep the virtual device rooted.

    0 讨论(0)
提交回复
热议问题