【问题标题】:How to transfer sharedpreferences file from internal storage to external storage?如何将 sharedpreferences 文件从内部存储传输到外部存储?
【发布时间】:2015-03-11 03:40:34
【问题描述】:

如何将sharedpreferences 复制到外部存储并保留xml format,以便以后可以共享偏好。

尝试读取sharedpreferences 并将string 保存到文件中,创建string 类型的JSON,但我需要xml。想过遍历app的内部存储,将文件复制到外部存储中,但这可能太复杂了。

真的很想知道是否有一种简单而明智的方式来转移`sharedpreferences。

【问题讨论】:

    标签: java android xml file sharedpreferences


    【解决方案1】:

    使用此代码,

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

    并使用它来检索,

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

    注意您只能使用此代码处理字符串类型首选项,

    【讨论】:

    • 它有效,但现在我必须重新设计其他部分的代码解析为整数类型,因为一切都是字符串。
    • 当然,请不要忘记与我们分享,
    • 更改移动 editor.commit();在循环之外。不需要提交每个值。
    【解决方案2】:

    要从文件中加载共享首选项,试试这个

    
    private fun loadSharedPreferences(src: File, sharedPreferences : SharedPreferences) {
        try{
          var fileInput : FileInputStream = FileInputStream(src)
          val properties : Properties = Properties()
          properties.loadFromXML(fileInput)
          fileInput.close()
          val enuKeys = properties.keys()
          val editor = sharedPreferences.edit()
          while (enuKeys.hasMoreElements()){
            var key = enuKeys.nextElement() as String
            var value = properties.getProperty(key)
            when {
              key.contains("string",true) -> {    //in my case the key of a String contain "string"
                editor.putString(key, value)
              }
              key.contains("int", true) -> {    //in my case the key of a Int contain "int"
                editor.putInt(key, value.toInt())
              }
              key.contains("boolean", true) -> {  // //in my case the key of a Boolean contain "boolean"
                editor.putBoolean(key, value.toBoolean())
              }
            }
    
            editor.apply();
          }
        }catch ( e : FileNotFoundException) {
          e.printStackTrace();
        } catch ( e : IOException) {
          e.printStackTrace();
        }
    
      }
    
    

    【讨论】:

      猜你喜欢
      • 2023-03-05
      • 2015-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-26
      相关资源
      最近更新 更多