Copy the shared preferences XML file from /data on Samsung device failed

前端 未结 6 786
粉色の甜心
粉色の甜心 2020-12-09 20:46

There\'s an exporting feature in my application. It\'s just a copy operation since all my settings are store in shared preference.

I just copy the xml file from /da

6条回答
  •  误落风尘
    2020-12-09 21:05

    Best way to get valid path on all devices - run method Context.getSharedPrefsFile defined as:

    /**
     * {@hide}
     * Return the full path to the shared prefs file for the given prefs group name.
     *
     * 

    Note: this is not generally useful for applications, since they should * not be directly accessing the file system. */ public abstract File getSharedPrefsFile(String name);

    Because of it hidden need use reflection and use fallback on fail:

    private File getSharedPrefsFile(String name) {
        Context context = ...;
        File file = null;
        try {
            if (Build.VERSION.SDK_INT >= 24) {
                try {
                    Method m = context.getClass().getMethod("getSharedPreferencesPath", new Class[] {String.class});
                    file = (File)m.invoke(context, new Object[]{name});
                } catch (Throwable e) {
                    Log.w("App TAG", "Failed call getSharedPreferencesPath", e);
                }
            }
    
            if (file == null) {
                Method m = context.getClass().getMethod("getSharedPrefsFile", new Class[] {String.class});
                file = (File)m.invoke(context, new Object[]{name});
            }
        } catch (Throwable e) {
            Log.w("App TAG", "Failed call getSharedPrefsFile", e);
    
            file = new File(context.getFilesDir(), "../shared_prefs/" + name + ".xml");
        }
        return file;
    }
    

    On some Samsungs implements like this:

    public File getSharedPrefsFile(String paramString) {
        return makeFilename(getPreferencesDir(), paramString + ".xml");
    }
    
    private File getPreferencesDir() {
        synchronized (this.mSync) {
            if (this.mPreferencesDir == null) {
                this.mPreferencesDir = new File("/dbdata/databases/" + getPackageName() + "/", "shared_prefs");
            }
            File localFile = this.mPreferencesDir;
            return localFile;
        }
    }
    

    On other Android like this:

    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");
    }
    

    After while Google change API for level 24 and later: https://android.googlesource.com/platform/frameworks/base/+/6a6cdafaec56fcd793214678c7fcc52f0b860cfc%5E%21/core/java/android/app/ContextImpl.java

提交回复
热议问题