0

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)
Joe
  • 253
  • 1
  • 7
  • 20
  • "but it's the only way I can think of to solve my problem" - How about saving the file name, or resource identifier, or whatever it is you're using to get the image? – Mike M. Apr 25 '17 at 23:45
  • Ok, thanks for the suggestions! I'm using drawbables to get the image. Could I save a the path somehow? or the drawable name? I'm just not sure how to then get the image from Shared Preference. Do you know how to save one of your suggestions? And that it would then keep the state. Cheers @MikeM. – Joe Apr 25 '17 at 23:51
  • If you mean the resource drawables in your app, then each `R.drawable` is actually just an `int`. It'd be much simpler, and faster, to save that to `SharedPreferences`, and then just call `setImageResource()` with the value retrieved. There's a basic `SharedPreferences`/`int` example here: http://stackoverflow.com/a/16194592. – Mike M. Apr 25 '17 at 23:57
  • @MikeM.Thanks Mike. I'll give this a go and let you know how it goes! Cheers mate – Joe Apr 26 '17 at 00:13

2 Answers2

1

Just store the drawable id in SharedPreferences and refer it.

    // store drawable id in SharedPreferences
    SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(context);
    SharedPreferences.Editor editor = sharedPref.edit();
    editor.putInt("drawableId", R.drawable.btn_background);
    editor.commit();

    // Read from sharedPref and set background
    int drawableId = sharedPref.getInt("drawableId", 0);

    ImageButton imageButton = (ImageButton) findViewById(R.id.imageButton);
    imageButton.setBackgroundResource(drawableId);
Naween Niroshan
  • 558
  • 4
  • 8
  • Thanks for the answer Naween! This looks very promising. I'll try it now and let you know. Cheers – Joe Apr 26 '17 at 00:12
  • You are welcome. I tested the code before posting. Please let me know if there's any issue. – Naween Niroshan Apr 26 '17 at 00:50
  • Thanks, I'm getting a cannot resolve symbol with 'imageButton'. I've tried it as ImageButton but then the 'setBackgroundResource' comes up with a n error saying 'Non-static method cannot be referenced from a static context'. Perhaps it's because I replace 'context' in your code with 'this'. Any ideas? Cheers – Joe Apr 26 '17 at 01:49
  • First you have to get an instance of your ImageButton and then use setBackgroundResource. ImageButton imageButton = (ImageButton) findViewById(R.id.imageButton); imageButton.setBackgroundResource(drawableId); – Naween Niroshan Apr 26 '17 at 10:21
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/142729/discussion-between-joe-and-naween-niroshan). – Joe Apr 26 '17 at 12:38
0

Your'd better not store drawable id ,for every time compile your app, the id may be different.You could store drawable name in SharedPreferences and get those resources id by ResourcesUtils.

 public class ResourcesUtils {

        private static final String RES_ID = "id";
        private static final String RES_STRING = "string";
        private static final String RES_DRABLE = "drable";

        public static int getId(Context context,String resName){
            return getResId(context,resName,RES_ID);
        }


        public static int getStringId(Context context,String resName){
            return getResId(context,resName,RES_STRING);
        }


        public static int getDrableId(Context context,String resName){
            return getResId(context,resName,RES_DRABLE);
        }

        public static int getResId(Context context,String resName,String defType){
            return context.getResources().getIdentifier(resName, defType, context.getPackageName());
        }
    }
Cyrus
  • 8,995
  • 9
  • 31
  • 58