Hello. There is a file in JSON format. It contains an array of objects, each object contains a "name" field and an "image" field, in which the url is specified to load the image. I want to display the object name and image in the ListView. For this, I use the ImageManadger class from this article. I tried different ways, but nothing happens. A huge request to show how I can upload images from a file in ListView. Thank you in advance

try { JSONArray data = new JSONArray(result); ArrayList<HashMap<String, Object>> MyArrList = new ArrayList<HashMap<String, Object>>(); HashMap<String, Object> map; for(int i = 0; i < data.length(); i++){ JSONObject c = data.getJSONObject(i); map = new HashMap<String, Object>(); map.put("name", c.getString("name")); ImageView imgView = (ImageView) findViewById(R.id.imageView1); ImageManager.fetchImage(c.getString("image"), imgView); imgView.buildDrawingCache(); Bitmap bmap = imgView.getDrawingCache(); d = new BitmapDrawable(bmap); map.put("image",d); map.put("image2",c.getString("image")); MyArrList.add(map); } SimpleAdapter simpleadapter; simpleadapter = new SimpleAdapter(SecondActivity.this, MyArrList, R.layout.list, new String[] { "name", "image"}, new int[] { R.id.myName, R.id.imageView1}); listView.setAdapter(simpleadapter); } 

    1 answer 1

    Well, it's all very bad. 1. Downloading all pictures in a cycle and storing them in a dictionary - will crash outofmemory on weak devices. 2. Work in the main thread is bad. My advice is to use the excellent Picasso library.

    Then inherit from SimpleAdapter and in the getView (...) method write:

     Picasso.with(context) .load(url) .into((ImageView)view.findViewByID(R.id.imageView1)); 

    And the load in the cycle is removed altogether.

    • There is also a Universal Image Loader there and a cache to disk and to memory and a bunch of everything extra. - Yura Ivanov
    • one
      Well, it seemed to me that UIL was too heaped up, Picasso worked without unnecessary frills and could also use the cache in memory and on disk. Google itself advises to use Volley android.googlesource.com/platform/frameworks/volley for network requests including receiving images. - Deadkenny