How to change the android sharedPreferences save path? - android

I want to change the android sharedPreferences save path,the sharedPreferences save in /data/data/xxx.xxx.xxx/shared_prefs,i want to change path to /sdcard. how i do?

You cannot modify where shared preferences are stored. Since shared preferences are simply stored in an XML file, you are welcome to read and write XML data on external storage as you see fit.
BTW, never use /sdcard. Use Environment.getExternalStorageDirectory(). /sdcard is not the correct path for over a third of existing Android devices.

Related

can a sharedpref file packageName_preference.xml can be created under different dir than default directory

by default shareprefs are stored in as below in app data directory:
/data/data//shared_prefs/packagename_preferences.xml
how can we create sharedpref as below:
/data/data//shared_prefs/customDir/CustomName_preferences.xml
?
Unfortunately, Android doesn't provide a way to choose SharedPreferences directory, but you can do so by creating a custom database file with your own algorithm.
However, why would you want to create subfolders inside "shared_prefs" folder? The user won't notice it, it's only for you.
If you need to create a compact and more reliable database, you can better use SQL Database.

Android SharedPreferences for files outside the shared_prefs directory

Specifically I want to create a shared preferences file in the no_backup directory, inside which it is recommended for Android 6.0 apps to store device specific information like installation id and GCM tokens.
However, the getSharedPreferences method in Context and getDefaultSharedPreferences method in PreferenceManager only allows me to modify files inside the shared_prefs directory.
Is there any solution to store simple preferences in the no_backup folder without resorting to custom solutions?
find the app files directory by getFilesDir, let it be filesDir
you can operate the shared_prefs files with path filesDir/../shared_prefs/<preference name>.xml

Saving a file to your own android application

When My application is running I want to capture some information and save this to a folder in my application rather than to the device. I have read online that it is not possible to save a file to your assets or res directory. However would it be possible to save it to another file in your application?I had a look at Shared Preferences but that seems to be only for Key Value pairs.
http://developer.android.com/guide/topics/data/data-storage.html
Writing to internal storage with MODE_PRIVATE should handle what I think you want..
You cannot store the files in the Assets or the Raw folder at run time.
Instead if you have image/video/audio/textual files you can store the files in your SD card and store the path of those files in your file in Database or shared preferences.
After installation each application creates a storage located in '/data/data/com.your.app.package.name'. Save whatever you want in that place.

does variables in shared preferences share accross applications?

I want to share variable between two android application so I stored it in shared preference but when I reinstalled the application that store the variable the second application didn't see the variable, what is the best way to share variable between two application and dealing with situation when one of them will be reinstalled
I used this code
SharedPreferences globals_prefs = ctx.getSharedPreferences("globals_prefs", Context.MODE_WORLD_WRITEABLE);
The SharedPreferences in one application is stored as a prefs.xml file in a separate folder dedicated for you app in the Android File System. So when you uninstall an app, this folder also is deleted, thereby, all Preferences made by your app is removed. So a better way is to use Files to store it in the sdCard.
Note: currently this class does not support use across multiple processes. This will be added later.
From android dev documents
http://developer.android.com/reference/android/content/SharedPreferences.html
File or sqlite database on public directory would do the trick when sharing data
When you uninstall an app shared preference and internal cache is wiped out, so the only way is to use external storage.

can we store shared preference on SD CARD

My android application is getting large and we want to store it in SD CARD. can i store shared preference in SD CARD along with my application
You cannot modify where shared preferences are stored. Since shared preferences are simply stored in an XML file, you are welcome to read and write XML data on external storage as you see fit.

Categories

Resources