0

I'm trying to get an image from an url and save that image as string to the sharedPreferences. (I don't wanna save the image as it is. so sharedpreferences is the only option)

I'm setting the image to my imageview from url using a chunk of code copied from stackoverflow.

class DownloadImageFromInternet(var imageView: ImageView) :
        AsyncTask<String, Void, Bitmap?>() {

        override fun doInBackground(vararg urls: String): Bitmap? {
            val imageURL = urls[0]
            var image: Bitmap? = null
            try {
                val `in` = URL(imageURL).openStream()
                image = BitmapFactory.decodeStream(`in`)
            } catch (e: Exception) {
                Log.e("Error Message", e.message.toString())
                e.printStackTrace()
            }
            return image
        }

        override fun onPostExecute(result: Bitmap?) {
            imageView.setImageBitmap(result)
            imageView.invalidate()
        }
    }

So, I need to get the image from the imageview and save that to a variable. I've seen some stackoverflow pages where the say we have to invalidate the imageview before we can use it. Though I'm invalidating, I'm getting the previous image set to the imageView (as I need the image as soon as the image set to the imageview.

Now my question is that how can I get the bitmap from the imageView as soon as the image is set to that imageView?

Thanks in advance.

  • `. (I don't wanna save the image as it is. so sharedpreferences is the only option)` You downloaded an image file i think. You can save a file in normal storage. – blackapps Jan 20 '22 at 07:06
  • Why do you first make a string of your image file? A lot of work i woyld say. – blackapps Jan 20 '22 at 07:07
  • Further i wonder why you would use an intermediats ImageView to obtain a bitmap from the stuff behind the url. Please explain better what you have and what you want. – blackapps Jan 20 '22 at 07:10
  • I have an bitmap which is set to an imageView. as I'm a beginner, I'm to preferring to use storage and that kind of stuffs and keep the code simple. That's why I'm converting the image to string. – Arijit Paul Jan 20 '22 at 07:10
  • Beginners dont convert images to strings. I would not call that easy. You better explain what it is that you download from url. – blackapps Jan 20 '22 at 07:13
  • I'm using [something like this](https://stackoverflow.com/a/30258613/13536048) and passing an url to download image and set the image right there. – Arijit Paul Jan 20 '22 at 07:18
  • I've updated the question. – Arijit Paul Jan 20 '22 at 07:23
  • `image = BitmapFactory.decodeStream(`in`)` Well that is your bitmap. Use it. Dont put it in an ImageView first where you later try to extract it from. You are not telling what you do as apparently you download a jpg or png file. You could save that file directly to storage. – blackapps Jan 20 '22 at 07:29
  • @blackapps I think my brain was lost somehow that I could not think of that simple idea. Now, I've implemented the code successfully and it just works fine. – Arijit Paul Jan 21 '22 at 12:20

0 Answers0