I have a series of ImageButtons in my app. Through the app, the user can set the background of the ImageButton to a drawable file. This is working perfectly fine, when the user selects the image they would like and clicks a refresh button, the ImageButton's image changes.
The issue is, when I go to other activities the changed ImageButton's go and are returned to the default image.
I've been trying to do this through SharedPreferences. I've tried to get this working using all of the other similar questions on here with no luck.
I'm a complete beginner when it comes to SharedPreferences and I know that storing images isn't what it's built for, but it's the only way I can think of to solve my problem.
Any help would be MASSIVELY appreciated! Thanks in advance guys.
What I know about storing images with SharedPreferences: I know that I have to convert the drawbable to a bitmap and then encode that into Base64 (because SharedPreferences will accept strings). I have done this, and I think the encoding is working.
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
grey11.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
byte[] grey11base64 = byteArrayOutputStream.toByteArray();
final String encodedGrey11 = Base64.encodeToString(grey11base64, Base64.DEFAULT);
In my main activity (where the images are displayed), I currently have OnCreate and onStop methods and an onClick for the refresh button.
EDIT:
In my onCreate(Bundle savedInstantState):
final SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt("drawableAgreen", R.drawable.a);
editor.commit();
Inside my Refresh button:
int drawableId = sharedPref.getInt("drawableId", 0);
ImageButton.setBackgroundResource(drawableId)