Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/182.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 有没有办法存储SD卡的SharedReference?_Android_Sharedpreferences_Android Sdcard - Fatal编程技术网

Android 有没有办法存储SD卡的SharedReference?

Android 有没有办法存储SD卡的SharedReference?,android,sharedpreferences,android-sdcard,Android,Sharedpreferences,Android Sdcard,我写了一个应用程序,它有几个硬编码设置,比如fontSize或targetDirectory。我希望能够在不频繁的基础上更改这些类型的设置 SharedReferences似乎是一种方式,但我想共享此应用程序和设置,而且我的手机没有根目录 我的应用程序是一个个人工具,没有UI。它打开,完成它的工作,然后关闭。我可以创建一个相当于Windows.ini的文件并对其进行读/写,但这看起来很笨拙。让SharePreferences文件位于SD卡上,我可以访问它,而不是设备内存,我无法访问它,这似乎是可

我写了一个应用程序,它有几个硬编码设置,比如fontSize或targetDirectory。我希望能够在不频繁的基础上更改这些类型的设置

SharedReferences似乎是一种方式,但我想共享此应用程序和设置,而且我的手机没有根目录

我的应用程序是一个个人工具,没有UI。它打开,完成它的工作,然后关闭。我可以创建一个相当于Windows.ini的文件并对其进行读/写,但这看起来很笨拙。让SharePreferences文件位于SD卡上,我可以访问它,而不是设备内存,我无法访问它,这似乎是可行的


我不想备份这些首选项,只想编辑它们,或将它们复制到新设备。

默认情况下,SharedReferences文件存储在内部存储器中。您可以通过编程方式将其备份到SD卡

    File ff = new File("/data/data/"
             + MainActivity.this.getPackageName()
             + "/shared_prefs/pref file name.xml");

    copyFile(ff.getPath().toString(), "your sdcard path/save file name.xml");



private void copyFile(String filepath, String storefilepath) {
    try {
        File f1 = new File(filepath);
        File f2 = new File(storefilepath);
        InputStream in = new FileInputStream(f1);

        OutputStream out = new FileOutputStream(f2);

        byte[] buf = new byte[1024];
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        in.close();
        out.close();
        System.out.println("File copied.");
    } catch (FileNotFoundException ex) {
        System.out.println(ex.getMessage());
    } catch (IOException e) {
        System.out.println(e.getMessage());
    }
}
您可以在第一次启动时将其替换回,并在应用程序关闭时对其进行备份


引用:

SharedReference文件是内部存储中的xml文件。当然,您可以将它们复制到外部或可移动存储。可能是Zarul的副本--谢谢您的回答,但我想积极使用该文件,而不是备份它。我的理解是,我没有权限将文件写回/data目录。