I am trying to write a small bit of binary data as a string in SharedPreferences. I can make a big mistake in the encoding here, but this is what I am trying to do:
String str = new String("hi there!".getBytes(), "ISO-8859-1"); SharedPreferences p = context.getSharedPreferences("foo", MODE_PRIVATE); Editor e = p.edit(); e.putString("string", str); e.putBoolean("worked", true); e.commit(); ... later on after an app restart... // the shared prefs file will be empty upon the next app start: SharedPreferences p = context.getSharedPreferences("foo", MODE_PRIVATE); Log.d(TAG, "String value present?: " + p.getString("string", null)); Log.d(TAG, "Boolean flag present?: " + p.getBoolean("worked", false));
The commit () call returns true, everything looks fine. The next time I ran this demo application, I read the contents of this shared instance of prefs and it is completely empty. If I do not use the encoding "ISO-8859-1", then everything works as expected, the general prefs file has key values ββin it.
Can general prefs not work with a string using this encoding?
thanks
source share