代码之家  ›  专栏  ›  技术社区  ›  Kakey

android上的图像问题

  •  0
  • Kakey  · 技术社区  · 15 年前

    我正在从url下载图像并设置为图像视图。但是有时位图返回空值? 为什么会这样?。。 我的密码是

    Bitmap bm = null;
    URL myFileUrl =null;          
        try 
        {
             myFileUrl= new URL(url);
        }
        catch (MalformedURLException e) 
        {
             // TODO Auto-generated catch block
             e.printStackTrace();
        }
        try 
        {
             HttpURLConnection conn= (HttpURLConnection)myFileUrl.openConnection();
             conn.setDoInput(true);
             conn.connect();
             int length = conn.getContentLength();
             InputStream is = conn.getInputStream();
             bm = BitmapFactory.decodeStream(is);
    
        } 
        catch (IOException e) 
        {
             // TODO Auto-generated catch block
             e.printStackTrace();
        }
        return bm;
    
    2 回复  |  直到 15 年前
        1
  •  0
  •   Macarse    15 年前

    我建议您尝试一些已经可以工作的东西,而不是自己编写代码。 查一下机器人傅的 WebImageView

        2
  •  0
  •   Peter Knego    15 年前
    1. 确保从网络上获取图像数据。
    2. 确保BitmapFactory.decodeStream文件()不返回null(错误的图像数据)。

    提示: 从后台线程运行网络代码。使用 AsyncTask .