0

Ok guys, I can use shared preferences to save text so next time the app is loaded that text is displayed/restored

But is there a way to do this with Button Background Images?

2 Answers2

0

Don't know what exactly you wan't to do, but you can obviously save an image(Bitmap) as a byte array. Also you can compress it using Bitmap's compress method. This can be of some help if you are sure about using shared prefs for saving image.

Community
  • 1
  • 1
Imdad
  • 683
  • 1
  • 9
  • 27
  • Well the button and all relate to image views so I guess it is the image views I want saving. I was trying to save the image path and recall it on start up. The user defines the image from preset selections within the app. And I just want the app to always remember the selections any time is closes and recall it on start up – Daniel Palfrey Feb 15 '16 at 08:16
  • Then in that case, you can save the name of image with button name as key in shared prefs and recall on startup! No point in saving the whole image. – Imdad Feb 15 '16 at 09:25
  • Cheers will IGive this a go when I get home and let you know – Daniel Palfrey Feb 15 '16 at 09:41
  • Ok, after running preliminary tests it looks like it will work but I am having trouble getting the image location/name from the ImageView – Daniel Palfrey Feb 15 '16 at 20:56
  • never mind, I found it, I had one small piece of bad code. I will share the solution later when I get a little more time – Daniel Palfrey Feb 15 '16 at 21:09
  • Because the images are stored local I just set boolean to trigger event for r.drawable.image name and then saved to presences. Not ideal and I assume a little process heavy but it does the job – Daniel Palfrey Feb 16 '16 at 09:07
0
ImageView iv = (ImageView) findViewById(R.id.imageView);
        Intent intent = getIntent();
        Boolean changeImageBg = intent.getBooleanExtra("change", false);
        if (changeImageBg) {
            iv.setImageResource(R.drawable.arrow_up_left_2small);
            SharedPreferences sharedpreferences = getSharedPreferences("done", Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = sharedpreferences.edit();
            String string;
            string = "arrow_up_left_2small";
            editor.putString("aul", string);
            editor.commit();

Ok, so above is the saving side. Since my image is part of the app I was able to save it easily once I understood what to do. I will be using boolean to trigger differnt images based on the active boolean I will be able to trigger the save for the appropriate image, I will at some point look at passing this code over the app so I can use less code.

SharedPreferences prefs = this.getSharedPreferences("done", Context.MODE_PRIVATE);

        String imageName = prefs.getString("aul", "");
        int resID = getResources().getIdentifier(imageName, "drawable", getPackageName());
        ImageView image = (ImageView) findViewById(R.id.imageView);
        image.setImageResource(resID);
    }

Here is where I call the file path and use it on create to re apply the pictures