To upload an image I use:

downloadImage

public static Bitmap downloadImage(String iUrl) { Bitmap bitmap = null; HttpURLConnection conn = null; BufferedInputStream buf_stream = null; try { Log.v(TAG, "Starting loading image by URL: " + iUrl); conn = (HttpURLConnection) new URL(iUrl).openConnection(); conn.setDoInput(true); conn.setRequestProperty("Connection", "Keep-Alive"); conn.connect(); buf_stream = new BufferedInputStream(conn.getInputStream(), 8192); bitmap = BitmapFactory.decodeStream(buf_stream); buf_stream.close(); conn.disconnect(); buf_stream = null; conn = null; } catch (MalformedURLException ex) { Log.e(TAG, "Url parsing was failed: " + iUrl); } catch (IOException ex) { Log.d(TAG, iUrl + " does not exists"); } catch (OutOfMemoryError e) { Log.w(TAG, "Out of memory!!!"); return null; } finally { if ( buf_stream != null ) try { buf_stream.close(); } catch (IOException ex) {} if ( conn != null ) conn.disconnect(); } return bitmap; } 

To update the listview

try {JSONObject jsonResponse = new JSONObject (response.toString ()); JSONArray jsonMainNode = jsonResponse.getJSONArray ("items"); // JSONArray Data = jsonResponse.getJSONArray ("snippet");

 for(int i = 0; i<jsonMainNode.length();i++){ JSONObject jsonChildNode = jsonMainNode.getJSONObject(i); String name = jsonChildNode.optString("kind"); String number = jsonChildNode.optString("etag"); JSONObject item = jsonMainNode.getJSONObject(i); JSONObject snippet = item.getJSONObject("snippet"); String title = snippet.getString("title"); String channelTitle = snippet.getString("channelTitle"); String pubDate = snippet.getString("publishedAt"); JSONObject thumbs = snippet.getJSONObject("thumbnails"); JSONObject thumb = thumbs.getJSONObject("default"); final String ico = thumb.getString("url"); new Thread(new Runnable() { public void run() { bmp = ImageManager.downloadImage(ico); } }).start(); countryList.add(createEmployee(title,channelTitle,pubDate, bmp)); } simpleAdapter.notifyDataSetChanged(); 

Listview update occurs, but it does not load pictures. Update ListView occurs on the button

  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky

1 answer 1

here you created a new stream in which you uploaded a picture and it remained in it

 new Thread(new Runnable() { public void run() { bmp = ImageManager.downloadImage(ico); } }).start(); 

you need to create a callback in downloadImage, that you have finished downloading a picture

 interface SuccessLoadImg { void onSuccesLoad(Bitmap bitmap); } public static Bitmap downloadImage(String iUrl, SuccessLoadImg successLoad) { ... successLoad.onSuccesLoad(btm); .. } new Thread(new Runnable() { public void run() { bmp = ImageManager.downloadImage(ico, new SuccessLoadImg() { onSuccesLoad(Bitmap bitmap) { countryList.add(createEmployee(title,channelTitle,pubDate, bmp)); } }; } }).start(); 

But I would recommend uploading pictures directly to the adapter, through Picasso and faster and less code

  • Thanks for the idea! - blatube.com