Save SharedPreferences files into custom dir or get default SharedPreferences dir

Is it possible to save SharedPreferences files into custom dir? Let’s say into /data/data/package.name/my_prefs/.

Or is it possible to retrieve the directory SharedPreferences are saved by default to?

  • Warning on EditText
  • Android list view Right / Left swipes like call logs
  • Use firefox as render engine for Android webview
  • Handling lists with RxJava and Retrofit in android
  • onFailedToReceiveAd(Ad request successful, but no ad returned due to lack of ad inventory when using admob with adwhirl
  • Setting Android fragment enter and exit transitions?
  • P.S. Hardcoding the path /data/data/package.name/shared_prefs/ is not the solution.

    Related posts:

    Change default webkit on Apache Cordova - Android
    Host name may not be null in HttpResponse execute for android
    Android MVP - should an Activity be a View or Presenter?
    java.lang.IllegalStateException: YouTubeServiceEntity not initialized error when using YouTubePlayer...
    Making an android Python service to run in suspend state
    How to get uid value of an android application from a list displayed in a spinner?
  • Bluetooth Low Energy Connection Parameters for Android, iOS and Win8
  • Why aren't getters preferred when accessing variables between activities?
  • List all camera images in Android
  • Android: Prevent multiple onClick events on a button (that has been disabled)
  • onActivityResult doesn't work?
  • drawable vs. drawable-ldpi/drawable-mdpi
  • 2 Solutions collect form web for “Save SharedPreferences files into custom dir or get default SharedPreferences dir”

    Or is it possible to retrieve the directory SharedPreferences are
    saved by default to?

    Yes.

    This is basically the dataDir /shared_prefs which you can get from the ApplicationInfo object (which in turn you can get from the PackageManager). (Also, it might be the same as the “getFilesDir” dir you can get easily from Context itself? Not sure, didn’t check that.)

    From the source, starting with Context.getSharedPreferences (ContextImpl source):

    public SharedPreferences getSharedPreferences(String name, int mode) {
            SharedPreferencesImpl sp;
            File prefsFile;
            boolean needInitialLoad = false;
            synchronized (sSharedPrefs) {
                sp = sSharedPrefs.get(name);
                if (sp != null && !sp.hasFileChangedUnexpectedly()) {
                    return sp;
                }
                prefsFile = getSharedPrefsFile(name);
    ...
    
    public File getSharedPrefsFile(String name) {
            return makeFilename(getPreferencesDir(), name + ".xml");
        }
    
    private File getPreferencesDir() {
            synchronized (mSync) {
                if (mPreferencesDir == null) {
                    mPreferencesDir = new File(getDataDirFile(), "shared_prefs");
                }
                return mPreferencesDir;
            }
        }
    
    private File getDataDirFile() {
            if (mPackageInfo != null) {
                return mPackageInfo.getDataDirFile();
            }
            throw new RuntimeException("Not supported in system context");
        }
    

    And “mPackageInfo” is an instance of LoadedApk:

    public File getDataDirFile() {
            return mDataDirFile;
        }
    
    mDataDirFile = mDataDir != null ? new File(mDataDir) : null;
    
     mDataDir = aInfo.dataDir;
    

    And that brings us back to ApplicationInfo.

    I’d say if you don’t want to rely on the convention /data/data/<package_name>/shared_prefs then it should be safe to get the “dataDir” and rely on “shared_prefs” from there?

    Or is it possible to retrieve the directory SharedPreferences are
    saved by default to?

    See this answer to know how get path in safe way: https://stackoverflow.com/a/33849650/1504248

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.