2010-06-07 36 views

回答

6

有很多方法可以實現您的請求。基本上你必須用urlrequest下載圖像,然後使用InputStream創建一個Bitmap對象。

只是一個示例代碼:

URL url = new URL("http://asd.jpg"); 
     URLConnection conn = url.openConnection(); 
     conn.connect(); 
     InputStream is = conn.getInputStream(); 


     BufferedInputStream bis = new BufferedInputStream(is); 

     Bitmap bm = BitmapFactory.decodeStream(bis); 

     bis.close(); 
     is.close(); 

後,你獲得你可以用它在你的ImageView例如

3

只是另一種方法的位圖對象從URL下載圖像

try { 
    Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL("http://abc.com/image.jpg").getContent()); 
} catch (MalformedURLException e) { 
    e.printStackTrace(); 
} catch (IOException e) { 
    e.printStackTrace(); 
}