Android:关于fragmvent的Listview,从url设置图像

时间:2016-11-13 18:02:50

标签: android android-bitmap

如何使用来自网址的图片更新ListViev

使用以下方式下载图片

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;
}

我的ListView在单击按钮时侦听,然后更新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更新,但没有图片。如果我使用@drawable中的图像就可以了。

1 个答案:

答案 0 :(得分:0)

问题是在尝试使用之前,位图尚未完成下载。当你在一个线程实例上调用start()时,该方法立即返回它被调用的线程,而新线程关闭并执行run方法(它自己或{{1}的方法提供为构造函数参数)。

Runnable立即返回时,您会在下载位图之前直接创建员工。因此,start()bmp,且无法显示图片。

您需要设置代码,以便下载图像后,可以在相应的视图中进行设置。