注意:該方法必須要在子線程中調用,因為涉及網絡請求
public Bitmap getBitmap(String url) { Bitmap bm = null; try { URL iconUrl = new URL(url); URLConnection conn = iconUrl.openConnection(); HttpURLConnection http = (HttpURLConnection) conn; int length = http.getContentLength(); conn.connect(); // 獲得圖像的字符流 InputStream is = conn.getInputStream(); BufferedInputStream bis = new BufferedInputStream(is, length); bm = BitmapFactory.decodeStream(bis); bis.close(); is.close();// 關閉流 } catch (Exception e) { e.printStackTrace(); } return bm; }