2010-11-09 93 views
0

我从url下载图像并设置为Imageview.but有时候位图返回null? 为什么会这样?.. 我的代码是android上的图像问题

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; 
+0

是否会抛出这些异常?如果有,您可以发布堆栈跟踪和/或异常类型和消息。它可能是你的Url格式不正确或某种IO异常被抛出,你无所事事“处理”它。 – Khalos 2010-11-09 11:13:47

+0

你可以检查你的Logcat,确保你是这个Error Skia Decoder,返回False。 – 2010-11-09 11:14:16

回答

0

我建议你尝试一些已经工作而不是做你自己的代码。 检查droid-fu的WebImageView

0
  1. 确保您从网络获取图像数据。
  2. 确保BitmapFactory.decodeStream()不返回空(错误的图像数据)。

小费: 从后台线程运行网络代码。使用AsyncTask