1

so I'm trying to save an Imageview to sharedpreference so when the user chose an image from the app it self, the Image will be saved and set to the imageview id if that makes sense! and when the user exit my app and reopen it the image will be the same one he saved?

I tried this code but it's giving me errors such as for input string " "

My SharedPreference

CompassBtn.setOnClickListener{


        val  prefCompass = getSharedPreferences("Drawable", Context.MODE_PRIVATE)
        val editor = prefCompass.edit()
        editor.putInt("ic_compass", ic_compass.setImageResource(R.drawable.ic_compass1).toString().toInt())
editor.commit()
    }

**this is how am trying to retrieve it **

   val prfCompass = getSharedPreferences("Drawable", Context.MODE_PRIVATE)
    prfCompass.getInt("ic_compass", 0)

please help and thanks in advance

Abood
  • 226
  • 1
  • 2
  • 14
  • You are trying to save an image, so this means the image is being loaded from the internet(URL). If this is the case you should use Glide or Picasso, which are android-third-party-libraries for downloading, loading, and handling caching. Also, if the image changes in the Remote, it will take care of that. – Amit Gupta May 16 '20 at 05:11
  • I have the image in the drawable file @AmitGupta – Abood May 16 '20 at 12:33
  • Please check the answer I have added. Hope that helps you. – Amit Gupta May 16 '20 at 16:16

2 Answers2

2

First of all, if you are new to Android Development, please check Picasso for uploading images. In short, it makes it easier/faster to upload images using resource id/url.

Your question really depends on the type of images you want user to select in the future.

1) If all of the images that can be selected are already in the application you can just save the resource id of image in SharedPreferences as an int

val sharedPref: SharedPreferences = context.getSharedPreferences("PREFERENCE_NAME", Context.MODE_PRIVATE)

     // Resource id is the int under drawables folder ->R.drawable.myImage
     fun save(KEY_NAME: String, value: Int) {
            val editor: SharedPreferences.Editor = sharedPref.edit()

            editor.putInt(KEY_NAME, value)

            editor.apply()
        }
   fun getInt(KEY_NAME: String): Int {

        return sharedPref.getInt(KEY_NAME, 0)
    }

2) If you are letting user to select from gallery (this is the tricky part) inside onActivityResult(which gets called after user selects an image with the argument, Intent data, that includes the information on image). Accessing the data of intent (data.getData()) will give you the URI. Then you need to find the path of the image (path where the image is stored in user's phone) and save it to SharedPreferences. I will leave getting the path of image as a challenge to you. And when you want to upload the image you can just;

  Bitmap bitmap = BitmapFactory.decodeFile(imagePath);
                    Drawable drawable = new BitmapDrawable(getResources(), bitmap);
                    myLayoutItem.setBackground(drawable);

3) If you have a server you can just upload your images there and store the url as a string in SharedPreferences/associate url with an image attribute of user. And use Picasso to display the image.

Prethia
  • 1,183
  • 3
  • 11
  • 26
1

As you have mentioned in the comment that you have the images in the res/drawable folder, and you want to save the selected image by the user and load the same when the user reopens the app.

So, the thing you have to do is save the resourceId in the preference and use that value.

It will be something this.

        //Suppose this is your selected drawable, you need to save its resourceId to shared preferences which is INT value
        val resourceId: Int = R.drawable.your_drawable_image 

        //save resouceId to sharedPreference

        //You can do this to set the Image in the ImageView
        your_imageview.setImageDrawable(getDrawable(resourceId))

I hope this will be of any help to you.

Amit Gupta
  • 633
  • 1
  • 8
  • 19
  • can you please tell me how do I implement it to the getInt method and display it from getInt? – Abood May 16 '20 at 18:07
  • You mean saving and retrieving Int value of resourceId from shared preferences? Please look at this article https://blog.mindorks.com/android-shared-preferences-in-kotlin – Amit Gupta May 17 '20 at 03:39
  • the blog you linked out is for a getBoolean method not get Int – Abood May 18 '20 at 19:08
  • 1
    For that, Please have a look at this, https://stackoverflow.com/questions/16194567/android-sharedpreferences-how-to-save-a-simple-int-variable – Amit Gupta May 19 '20 at 04:33