简体   繁体   中英

How to transfer sharedpreferences file from internal storage to external storage?

How to copy sharedpreferences to external storage keeping xml format , so later it could be possible to share preferences.

Tried to read sharedpreferences and save as a string into file, created JSON type of a string , but I need an xml . Thought of traversing through app's internal storage and copy file and put it in external storage, but that could be too complex.

Just really would want to know if there is an easy and sensible way to transfer `sharedpreferences.

Use this code,

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

and to retrive use this,

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

NOTE You can handle only String type preferences with this code,

for load shared preference from file try this


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

  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM