My situation is that: I have a button that change background of my parent layout with a image of SD (this work fine). Then I like save these image in a SharedPreference to allow the user start my app with their background image, and not my default background image. I save the image this way:
SharedPreferences.Editor editor = prefs.edit();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
yourSelectedImage.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bm is the bitmap object
byte[] b = baos.toByteArray();
editor.putString("background", Base64.encodeToString(b, Base64.DEFAULT ));
And I retrieve on this way (this code is in onCreate):
prefs = getSharedPreferences("Mis preferencias",Context.MODE_PRIVATE);
String fondo = prefs.getString("background", "vacio");
if(!fondo.equals("vacio")){
byte[] b = Base64.decode(fondo, Base64.DEFAULT);
InputStream is = new ByteArrayInputStream(b);
Bitmap yourSelectedImage = BitmapFactory.decodeStream(is);
BitmapDrawable bd = new BitmapDrawable(getResources(), yourSelectedImage);
View view = findViewById(R.id.padre);
view.setBackgroundDrawable(bd);
}
Is the first time that use sharedpreferences and play with images in base64, so I'm little stuck with this, if I kill my app and restart, the default backgrounds appear, instead the custom. Any help? thanks and sorry for my english.