2

I need to get image URL from JSON file. How to set ImageView src to be URL from file?

In the end I need to create grid of images. First I want to make one ImageView with src from JSON file. I managed to read JSON file, I can print URL using TextView, but can't figure out how to make that string an URL.

JSON file structure.

{
    "name": "dog0",
    "link": "https://images.unsplash.com/photo-1503256207526-0d5d80fa2f47?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=633&q=80"
}

This is how I read file, adding url's to an array.

fun readFile() {
        var json : String? = null
        try {
            val inputStream:InputStream = assets.open("dogs.json")
            json = inputStream.bufferedReader().use{it.readText()}

            var jsonarr = JSONArray(json)

            for (i in 0..jsonarr.length()-1){
                var jsonobj = jsonarr.getJSONObject(i)
                arr.add(jsonobj.getString("link"))
            }
            var adpt = ArrayAdapter(this, android.R.layout.simple_list_item_1,arr)
        }
        catch(e: IOException) {

        }
    }

I know it's possible to use external libraries to do such a thing, but I want to solve this problem without external stuff.

How to make ImageView have src from URL?

Vania
  • 21
  • 1
  • 3
    "I want to solve this problem without external stuff" -- **please please please** use an existing image-loading library, such as Glide or Picasso. There are a lot of things that you need to do to handle this properly: network I/O, background threading, dealing with row recycling in your `ArrayAdapter`, image caching, etc. – CommonsWare Jun 07 '19 at 18:39
  • 1
    Possible duplicate of https://stackoverflow.com/questions/2471935/how-to-load-an-imageview-by-url-in-android – Marcus Mann Jun 07 '19 at 18:40
  • One of the skills of good developer suppose to be saving time if he can and spend it necessarily . So MOST of developers don't do that process without Picasso/Glide thats why I don't think any body could help you with that – Bo Z Jun 07 '19 at 20:22

0 Answers0