Preferences android studio где
Перейти к содержимому

Preferences android studio где

  • автор:

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

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 Eclipse, but I’m debugging on Android Studio. My phone is rooted. I read that having root access is important to read these types of files. If there is no way, then I will look up how to access the info through my program then output it to log cat. Hopefully, though, I can just view the file on the phone directly as it is much simpler. Thanks.

asked May 13, 2014 at 15:35
1,681 2 2 gold badges 11 11 silver badges 3 3 bronze badges

Install ES File Explorer and enable root browsing. You will be able to open it directly on the phone then.

May 13, 2014 at 15:37
try using adb pull to fetch the file to the desktop. adb pull /data/data//shared_prefs/prefs.xml May 13, 2014 at 15:51

OR goto file explorer and in the file tree, find your app’s data folder under /data/data/com.your-package/shared_prefs. The preference file will be there, as an XML. Copy it from the device and enjoy. In the upper right corner of the File Explorer view, there are two icon buttons — one with a disk and one with a mobile phone. These buttons will allow you to copy files from and to the phone/emulator, respectively.

May 13, 2014 at 15:53
For some reason my prefs xml file is called _preferences.xml
Jan 7, 2016 at 17:18

15 Answers 15

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

enter image description here

It looks something like this

enter image description here

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.

enter image description here

answered Sep 16, 2018 at 9:28
12.9k 11 11 gold badges 47 47 silver badges 64 64 bronze badges
Open Device File Explorer (Lower Right of screen).
Dec 17, 2018 at 10:06
is it possible to edit and save the file ?
Feb 20, 2021 at 5:31
You can «Save As . » file then modify it and replace the original file.
Nov 9, 2021 at 8:31
@AndreyLiashuk how do you replace the original file?
Dec 2, 2021 at 12:24

@Daniel I just Delete the original file and then Upload the updated variant. Luckily Device File Explorer supports Delete and Upload operations.

Dec 3, 2021 at 15:02

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.

12.5k 4 4 gold badges 39 39 silver badges 44 44 bronze badges
answered Aug 30, 2014 at 18:50
George Dima George Dima
2,727 3 3 gold badges 17 17 silver badges 19 19 bronze badges
I can´t access this folder from Device Monitor. Is Root required?
Jul 22, 2015 at 7:48

Second method works for non-rooted devices. After you copy the file to sd card, you can exit adb and run adb pull «/sdcard/your_preferences_file» and you’ll get an xml file in your current directory you can inspect.

Jun 15, 2016 at 18:29

You can actually grab this info from an emulator without root, if you can debug that way. If you are on a physical device, you will need root.

Jun 29, 2016 at 21:15

look for device file explorer in the bottom right corner of AndroidStudio. It will provide you easy access to device file system

Aug 21, 2018 at 6:10
Using Device File Explorer, I can access the shared preferences without root access
Jun 11, 2019 at 13:55

UPDATE: Flipper

Flipper is a newer alternative from facebook. It has more features. And actively maintained

Stetho

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

  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.

enter image description here

  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/

You can also use @Jeffrey suggestion:

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

answered Nov 7, 2016 at 12:07
Maksim Turaev Maksim Turaev
4,155 1 1 gold badge 29 29 silver badges 42 42 bronze badges
Can this work on emulators? I can’t seem to make it work
Dec 5, 2017 at 21:54

@EduardoMaia It should. I use it with emulator every day. You can create a question with screenshots, so I will try to help you.

Dec 6, 2017 at 7:51

@EduardoMaia You can try to do the following: 1. Clone facebook stetho repository. 2. Import this project to LATEST Android Studio (File->Import->»choose settings.gradle») 3. agree to update gradle plugin. 4. Start Android Emulator (in my case API 24) 5. Launch stetho-sample (select it from run configuration menu next to green arrow 6. On your PC open latest Chrome browser 7. go by chrome://inspect address 8. chose your emulator in list. 9. In opened window go to Resources tab. 10. Find your shared preferences under LocalStorage menu on the left side. 11. In application play with boolean flag

Dec 6, 2017 at 8:26

I fixed my problem yesterday without needing to view the shared prefs. But I’ll try this step-by-step another day. Thanks.

Dec 6, 2017 at 13:24
Stetho is outdated. You would need to use Flipper or some other tools for this.
Dec 6, 2021 at 13:21

Android Studio -> Device File Explorer (right bottom corner) -> data -> data -> -> shared-prefs

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

answered Apr 20, 2018 at 9:35
1,879 2 2 gold badges 22 22 silver badges 28 28 bronze badges

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.

enter image description here

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 .

enter image description here

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.

answered Jul 29, 2017 at 18:32
Reaz Murshed Reaz Murshed
23.8k 13 13 gold badges 78 78 silver badges 98 98 bronze badges

Thanks to your post I learned about developer.android.com/studio/debug/device-file-explorer which is the new way to do the same you’re explaining here.

May 31, 2018 at 12:00

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); Mapprefs = 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) pref; > // Every new preference goes to a new line prefTextView.append(printVal + "\n\n"); > > // call loadPreferences() in the onCreate of your Activity. 

98 15 15 bronze badges
answered May 13, 2014 at 15:58
cYrixmorten cYrixmorten
7,110 3 3 gold badges 25 25 silver badges 33 33 bronze badges

This was very useful, but it didn’t quite fit my need. I added an edit that allows for multiple shared preference files

Aug 19, 2015 at 22:03
Thanks, I’m just having trouble figuring out how to accept the edits
Aug 20, 2015 at 10:02

It was denied by others. They said it should be its own answer, so I put it as a new answer. I originally just edited your answer because I was only able to do what I did when I seen your answer.

Aug 20, 2015 at 11:11
Alright 🙂 yeah occurred to me after that it had to be reviewed by others
Aug 20, 2015 at 11:54

To open shared preference in android studio

Open device explorer file from right side- data>data> >shared_prefs

find the attached image for more description

1,420 4 4 gold badges 13 13 silver badges 23 23 bronze badges
answered Aug 16, 2019 at 5:34
560 6 6 silver badges 13 13 bronze badges
where is attached image
Sep 27, 2020 at 11:50
In Android Studio 4.1 the path is data/data/[YOUR_APP_PACKAGE]/shared_prefs/
Jan 11, 2021 at 11:09

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 .

1,182 3 3 silver badges 16 16 bronze badges
answered Jan 21, 2016 at 7:34
user5291072 user5291072
129 1 1 silver badge 4 4 bronze badges

This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post — you can always comment on your own posts, and once you have sufficient reputation you will be able to comment on any post. — From Review

Jan 21, 2016 at 8:19
@LucaDetomi you know what I’m out of here
Jan 21, 2016 at 14:32

Your answer was flagged as too short. Answers should contain details that let users to understand «why» it could be a good answer and maybe the best one. I suggest you to add more details that help to understand principles and maybe to adapt it to other similar issues

Jan 21, 2016 at 14:41

In the Device File Explorer follow the below path :-

/data/data/com.**package_name**.test/shared_prefs/com.**package_name**.test_preferences.xml 

answered Sep 10, 2019 at 12:45
neelkanth_vyas neelkanth_vyas
192 1 1 silver badge 7 7 bronze badges

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 SetSET_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); MapprefMap = 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﹕ ------------------------------------ 

Where are shared preferences stored?

I am aware that shared preference.xml is stored in data, but where is it stored before being installed; in .smali format?

Dec 10, 2017 at 5:36

8 Answers 8

SharedPreferences are stored in an xml file in the app data folder, i.e.

/data/data/YOUR_PACKAGE_NAME/shared_prefs/YOUR_PREFS_NAME.xml 

or the default preferences at:

/data/data/YOUR_PACKAGE_NAME/shared_prefs/YOUR_PACKAGE_NAME_preferences.xml 

SharedPreferences added during runtime are not stored in the Eclipse project.

Note: Accessing /data/data/ requires superuser privileges

5,803 3 3 gold badges 29 29 silver badges 37 37 bronze badges
answered May 26, 2011 at 23:22
40.2k 9 9 gold badges 86 86 silver badges 108 108 bronze badges
The default shared preferences file would actually be: /data/data//shared_prefs/_preferences.xml . May 27, 2011 at 0:08

@inazaruk It is indeed the full package name, although I see mixed uses of /_preferences.xml and /.xml on devices. Perhaps it depends on the API level of the app?

May 27, 2011 at 0:12

Note that I was taking about Default shared preferences. It looks like filename was always the same for them. See initial commit for PreferenceManager.java , getDefaultSharedPreferences function here: android.git.kernel.org/?p=platform/frameworks/…. And it wasn’t changed in the latest version of PrefencesManager.jave too: android.git.kernel.org/?p=platform/frameworks/…

May 27, 2011 at 7:35

Any way to get the path from system, and not hard-code it? ContextImpl.java implements it in private getPreferencesDir func, it would be useful to get this at runtime somehow.

Mar 13, 2012 at 8:28

You can use new FIle(context.getFilesDir().getParent(), «shared_prefs»). It does work on Nexus 5, not sure about other manufactures.

May 14, 2015 at 18:39

Preferences can either be set in code or can be found in res/xml/preferences.xml. You can read more about preferences on the Android SDK website.

12.1k 6 6 gold badges 44 44 silver badges 65 65 bronze badges
answered May 26, 2011 at 23:06
34.5k 20 20 gold badges 113 113 silver badges 170 170 bronze badges

You have to create it — as long as you have created an Android project, you can then right click on the «res» directory and add a new folder called ‘xml’. Other «special» folders are anim, drawable, layout, menu, raw, and values.

May 26, 2011 at 23:10

I just tried to get path of shared preferences below like this.This is work for me.

File f = getDatabasePath("MyPrefsFile.xml"); if (f != null) Log.i("TAG", f.getAbsolutePath()); 

answered Feb 2, 2012 at 7:22
Ramesh Akula Ramesh Akula
5,720 4 4 gold badges 43 43 silver badges 67 67 bronze badges

Just to save some of you time.

On my Galaxy S v.2.3.3 Shared Preferences are not stored in: /data/data/YOUR_PACKAGE_NAME/shared_prefs/YOUR_PREFS_NAME.xml

but are now located in: /dbdata/databases/YOUR_PACKAGE_NAME/shared_prefs/YOUR_PREFS_NAME.xml

I believe they changed this in 2.3

answered Jul 28, 2011 at 11:19
android-overflow android-overflow
745 1 1 gold badge 7 7 silver badges 10 10 bronze badges

Is it same in 2.2? I cant find /data file anywhere in my project either in eclipse or project location.

Sep 11, 2011 at 10:44
@pramod not in eclipse — this is the path on the actual device file structure.
Sep 19, 2012 at 12:40

@Pramod see it in file explorer view and there in /data/data/YOUR_PACKAGE_NAME/shared_prefs/YOUR_PREFS_NAME.xml

Jan 28, 2013 at 13:26
/dbdata/ does not exists on my JB device
Jun 25, 2013 at 7:52

The data is stored on the device, in your application’s private data area. It is not in an Eclipse project.

answered May 26, 2011 at 23:23
90.7k 16 16 gold badges 140 140 silver badges 154 154 bronze badges

Shared Preferences are the key/value pairs that we can store. They are internal type of storage which means we do not have to create an external database to store it. To see it go to, 1) Go to View in the menu bar. Select Tool Windows. 2) Click on Device File Explorer. 3) Device File Explorer opens up in the right hand side. 4) Find the data folder and click on it. 5) In the data folder, you can select another data folder. 6) Try to search for your package name in this data folder. Ex: com.example.com 7) Then Click on shared_prefs and open the .xml file.

Hope this helps!

answered Nov 7, 2019 at 19:37
Chinmai Kulkarni Chinmai Kulkarni
147 1 1 silver badge 4 4 bronze badges

Use http://facebook.github.io/stetho/ library to access your app’s local storage with chrome inspect tools. You can find sharedPreference file under Local storage ->

enter image description here

answered Jun 19, 2018 at 6:21
Harshit Jain Harshit Jain
142 1 1 silver badge 4 4 bronze badges

save in /data/data/your_package_name/shared_prefs/

For see that you can use:

answered Apr 9, 2021 at 12:30
Rasoul Miri Rasoul Miri
11.3k 1 1 gold badge 68 68 silver badges 78 78 bronze badges

Highly active question. Earn 10 reputation (not counting the association bonus) in order to answer this question. The reputation requirement helps protect this question from spam and non-answer activity.

  • android
  • sharedpreferences

This question is in a collective: a subcommunity defined by tags with relevant content and experts.

    The Overflow Blog

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *