hcg_21 2012-10-10 06:16 采纳率: 0%
浏览 3542
已采纳

加载远程图像到ImageView,但没有加载上

我使用以下函数加载一个远程图像到ImageView。图像存在于URL,我想从URL中加载图像,但当我调用函数想显示图像时, ImageView仍是空白。为什么会出现这种情况啊?

public static void setImage(ImageView view, String url) throws IOException { 

        final URLConnection conn = new URL(url).openConnection(); 
        conn.connect(); 
        final InputStream is = conn.getInputStream(); 

        final BufferedInputStream bis = new BufferedInputStream(is, 100000); 

        final Bitmap bm = BitmapFactory.decodeStream(bis); 
        bis.close(); 
        is.close(); 

        view.setImageBitmap(bm); 
    } 
  • 写回答

2条回答 默认 最新

  • myc_100 2012-10-10 06:59
    关注

    通过下面的代码能够加载远程图像到ImageView:

     Bitmap bitmap = null;
    
        HttpGet httpRequest = null;
    
    
        try {
                URL url = new URL(your_url);
                httpRequest = new HttpGet(url.toURI());
    
                HttpClient httpclient = new DefaultHttpClient();
                HttpResponse response = (HttpResponse) httpclient.execute(httpRequest);
    
                HttpEntity entity = response.getEntity();
                BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity); 
                InputStream instream = bufHttpEntity.getContent();
                bitmap = BitmapFactory.decodeStream(instream);
        } catch (URISyntaxException e) {
                e.printStackTrace();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大