2010-11-09 89 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