0

Why my image not getting displayed in imageView?

    var image : Bitmap?=null

    longToast(obj?.image?.url.toString())

    try {
        image = BitmapFactory.decodeFile(obj?.image?.url.toString())
        imagePhoto.setImageBitmap(image)
        longToast("abc")
    } catch (e: Exception) {
        e.message
    }

The image is not null as I saw the toast displayed

https://...../tempory.jpg
John Joe
  • 12,412
  • 16
  • 70
  • 135
  • 1
    you call `decodeFile` - a "file" here is something in your local filesystem - not a resource pointed by `http://xxx` uri - you can use [picasso](http://square.github.io/picasso/) in such cases – pskink Jan 29 '19 at 08:27
  • 1
    try using `Glide` https://medium.com/@vlonjatgashi/using-glide-with-kotlin-5e345b557547 – karan Jan 29 '19 at 08:28
  • That is an url, so you need to download it first before showing it. Consider using image loader library like `Glide` to handle these tasks for you – Bach Vu Jan 29 '19 at 08:29
  • `decodeFile` can't handle internet locations – Vladyslav Matviienko Jan 29 '19 at 08:33

1 Answers1

1

BitmapFactory.decodeFile can not handle url

To load image from url you can use Glide, this library is recommended by Google. From doc:

Note: There are several libraries that follow best practices for loading images. You can use these libraries in your app to load images in the most optimized manner. We recommend the Glide library

Add Glide to your project:

dependencies {
  implementation 'com.github.bumptech.glide:glide:4.8.0'
  annotationProcessor 'com.github.bumptech.glide:compiler:4.8.0'
}

Then

Glide.with(context)
.load(obj?.image?.url.toString())
.into(imagePhoto)
Liar
  • 1,235
  • 1
  • 9
  • 19