Sharedpreferences File

Where are shared preferences stored?

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/<package_name> requires superuser privileges

How to create, read and write a sharedPreferences file?

You can create SharedPreferences in you project without finding it in the project directory. This is actually stored in your project folder in the app file system once created (/data/data/YOUR_PACKAGE_NAME/shared_prefs/YOUR_PREFS_NAME.xml).
But for creating, updating and retrieving you do not want to go there.

To Create SharedPreference

SharedPreferences.Editor editor = getSharedPreferences("MyPreference", MODE_PRIVATE).edit();
editor.putString("name", "Nathan");
editor.putInt("id", 1000);
editor.apply();

To retrieve

SharedPreferences prefs = getSharedPreferences("MyPreference", MODE_PRIVATE); 
String name = prefs.getString("name", "Blank Name"); //"Blank Name" the default value.
int idName = prefs.getInt("id", 0); // 0 is the default value.

Please go through the below link for more details.
https://developer.android.com/reference/android/content/SharedPreferences?hl=en

Delete SharedPreferences File

If you get SharedPreferences instance via Context.getSharedPreferences("X"), then your file will be named X.xml.

It will be located at /data/data/com.your.package.name/shared_prefs/X.xml. You can just delete that file from the location. Also check /data/data/com.your.package.name/shared_prefs/X.bak file, and if it exists, delete it too.

But be aware, that SharedPreferences instance saves all data in memory. So you'll need to clear preferences first, commit changes and only then delete preferences backing file.

This should be enough to implement your design decision.

What is the name of SharedPreferences file name?

I am using Android Studio and created a "Settings Activity".

Then you get your SharedPreferences via PreferenceManager.getDefaultSharedPreferences(). Replace:

SharedPreferences preferences = getSharedPreferences(PREF_FILE_NAME, MODE_PRIVATE);

with:

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);

How to transfer sharedpreferences file from internal storage to external storage?

Use this code,

SharedPreferences preferences=this.getSharedPreferences("com.example.application", Context.MODE_PRIVATE);
Map<String,?> keys = preferences.getAll();
Properties properties = new Properties();
for(Map.Entry<String,?> entry : keys.entrySet()){
String key = entry.getKey();
String value = entry.getValue().toString();
properties.setProperty(key, value);
}
try {
File file = new File("externalPreferences.xml");
FileOutputStream fileOut = new FileOutputStream(file);
properties.storeToXML(fileOut, "External Preferences");
fileOut.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

and to retrive use this,

try {
File file = new File("externalPreferences.xml");
FileInputStream fileInput = new FileInputStream(file);
Properties properties = new Properties();
properties.loadFromXML(fileInput);
fileInput.close();

Enumeration enuKeys = properties.keys();
SharedPreferences.Editor editor = preferences.edit();
while (enuKeys.hasMoreElements()) {
String key = (String) enuKeys.nextElement();
String value = properties.getProperty(key);
editor.putString(key, value);
editor.commit();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

NOTE You can handle only String type preferences with this code,

Edit and save shared preferences in Android Studio

I can access shared preferences in Android Studio from Device file explorer, but after editing the file, the changed values aren't saved. How can I save the changes?

You're not editing the files directly. When you open a file that's on the device, Android Studio is actually pulling a copy to your local machine.

For example, if I selected this random i_log file from my SDCard it ends up here on my local machine under Documents on Mac (shown in the top status bar in Android Studio).
Sample Image

If you want to save the changes back to the device, you need to "upload" the file back to the device.

  1. On the folder in the device explorer you want to move the file to, right-click.
  2. Select Upload..

Sample Image


  1. In the file chooser dialog, navigate to the local copy you edited.

Sample Image

AS will push that file back to the device.

how to add a new map or row to sharedpreferences file

You can't do that in SharedPreferences because it is a file based on a single map with key/values.

You have to use a database, with SQLite, Room, etc.

How to access Flutter's shared preference file?

I found a work-around, by directly modify the files in Android virtual machine:
Go to Android View -> Tool Windows -> device file explorer.

Here you will see all the files on the virtual phone device.

Navigate to your app folder, normally in data/data/com.example.yourprojectname

In shared_prefs folder, there is an XML file containing all the local key-value pairs, you can directly modify it, or delete it here.

PS: At the current stage, if the Flutter app has heavy features based on local (SQLite and shared preference), Android Studio is a much better-developing tool than VSCode, for much better virtual device inspection.



Related Topics



Leave a reply



Submit