【问题标题】:Android: Listview on fragmvent, set image from urlAndroid:片段中的Listview,从url设置图像
【发布时间】:2016-11-13 18:02:50
【问题描述】:

如何使用来自 url 的图像更新 ListViev

要下载我正在使用的图像:

下载图片

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 的图像,一切正常。

【问题讨论】:

    标签: android android-bitmap


    【解决方案1】:

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

    当start() 立即返回时,您可以在位图下载之前直接创建员工。因此,bmp 是 null,并且没有要显示的图像。

    您需要设置代码,以便在下载图像后将其设置在适当的视图中。

    【讨论】:

    • 谢谢你的想法
    猜你喜欢
    • 2014-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多