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

前端 未结 14 1022
甜味超标
甜味超标 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:16

    From Android Studio , start Android Device Monitor, go to File Explorer, and browse "/data/data/< name of your package >/shared_prefs/". You will find the XML there... and also you can copy it for inspection.

    If you have a non-rooted device it's not possible to do that directly from Android Studio. However, you can access the file with adb shell as long as your application is the debug version.

    adb shell
    run-as your.app.id
    chmod 777 shared_prefs/your.app.id_preferences.xml
    exit # return to default user
    cp /data/data/your.app.id/shared_prefs/your.app.id_preferences.xml /sdcard
    

    After that you can pull the file from /sdcard directory with adb.

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

    Another simple way would be using a root explorer app on your phone.

    Then go to /data/data/package name/shared preferences folder/name of your preferences.xml, you can use ES File explorer, and go to the root of your device, not sd card.

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

    In the Device File Explorer follow the below path :-

    /data/data/com.**package_name**.test/shared_prefs/com.**package_name**.test_preferences.xml
    
    0 讨论(0)
  • 2020-11-27 12:21

    You could simply create a special Activity for debugging purpose:

    @SuppressWarnings("unchecked")
    public void loadPreferences() {
    // create a textview with id (tv_pref) in Layout.
    TextView prefTextView;
    prefTextView = (TextView) findViewById(R.id.tv_pref);
        Map<String, ?> prefs = PreferenceManager.getDefaultSharedPreferences(
                context).getAll();
        for (String key : prefs.keySet()) {
            Object pref = prefs.get(key);
            String printVal = "";
            if (pref instanceof Boolean) {
                printVal =  key + " : " + (Boolean) pref;
            }
            if (pref instanceof Float) {
                printVal =  key + " : " + (Float) pref;
            }
            if (pref instanceof Integer) {
                printVal =  key + " : " + (Integer) pref;
            }
            if (pref instanceof Long) {
                printVal =  key + " : " + (Long) pref;
            }
            if (pref instanceof String) {
                printVal =  key + " : " + (String) pref;
            }
            if (pref instanceof Set<?>) {
                printVal =  key + " : " + (Set<String>) pref;
            }
            // Every new preference goes to a new line
            prefTextView.append(printVal + "\n\n");     
        }
    }
    // call loadPreferences() in the onCreate of your Activity.
    
    0 讨论(0)
  • 2020-11-27 12:23

    In Android Studio 3:

    • Open Device File Explorer (Lower Right of screen).
    • Go to data/data/com.yourAppName/shared_prefs.

    or use Android Debug Database

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

    Run the application in Emulator after you insert some data, just close the application.

    Now open the DDMS or Android Monitor and select your emulator, on the right side you can see the File Explorer, look for Data folder in it and look for your application package that you have created, in that you can find the shared preference file open it , you can see the XML file, click it and click the pull a file from the device button in the top right corner.

    The XML file will be saved in your desired location, then you can open it using any editor like notepad++ and can view the data you have entered.

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