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

3
  • 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. Commented May 16, 2020 at 5:11
  • I have the image in the drawable file @AmitGupta Commented May 16, 2020 at 12:33
  • Please check the answer I have added. Hope that helps you. Commented May 16, 2020 at 16:16

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

6 Comments

can you please give me any samples or tutorials on how I save the resource id?
Simply saving it as an integer should work. I have updated the answer
I tried your example and still it doesn't show the selected one out of the two images I have.
You should include your code in the question then. Saving resource id to shared preference should work. Your code includes an error somewhere else
where do I put the resource id tho?
|
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.

4 Comments

can you please tell me how do I implement it to the getInt method and display it from getInt?
You mean saving and retrieving Int value of resourceId from shared preferences? Please look at this article blog.mindorks.com/android-shared-preferences-in-kotlin
the blog you linked out is for a getBoolean method not get Int
For that, Please have a look at this, stackoverflow.com/questions/16194567/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.