2011-03-25 110 views
2

我想從網絡中從某個URL獲取圖像並將其顯示在我的ImageView中。 以下是我使用的代碼: -setImageURI/setimagebitmap從網絡中獲取圖像並在圖像視圖中顯示

Bitmap bm = null; 
    try { 
     URL aURL = new URL("stringURL"); 
     URLConnection conn = aURL.openConnection(); 
     conn.connect(); 
     InputStream is = conn.getInputStream(); 
     BufferedInputStream bis = new BufferedInputStream(is); 
     bm = BitmapFactory.decodeStream(bis); 
     bis.close(); 
     is.close(); 
    }catch (Exception e) { 
     // TODO: handle exception 
} 
    iv.setImageBitmap(bm); 

,但我不能讓圖像

回答

0

我認爲錯誤是

URL aURL = new URL("stringURL"); 

應該是不帶引號

URL aURL = new URL(stringURL); 

如果stringURL是有效的url它將工作...

希望它有幫助../

0

我覺得你可以使用下面的代碼,這對我很好。

String stringURL = "Your url here"; 

InputStream is = null; 
BufferedInputStream bis = null; 
Bitmap bmp = null; 

try { 
    URL url = new URL(stringURL); 
    URLConnection conn = url.openConnection(); 
    conn.connect(); 
    is = conn.getInputStream(); 
    bis = new BufferedInputStream(is); 
    bmp = BitmapFactory.decodeStream(bis); 

} catch (MalformedURLException e) { 

} catch (IOException e) { 

}catch (Exception e) { 

} finally { 
    try { 
     if(is != null) 
      is.close(); 
     if(bis != null) 
      bis.close(); 
    } catch (IOException e) { 

    } 
} 
iv.setImageBitmap(bmp); 
相關問題