BitmapFactory.decodeFile能否對一張來自HTTP的圖片進行解碼呢
BitmapFactory.decodeFile(String)沒有從網絡解碼圖片的能力。不過還有另外一個函數可以幫到你——BitmapFactory.decodeStream(InputStream)函數,具體實現請參考以下代碼:
01 |
// 準備一張網絡圖片,例如:[img]https://www.example.com/img.jpg[/img] |
02 |
URL aryURI = new URL(String);
|
03 |
// 取得連接 |
04 |
URLConntection conn = aryURI.openConnection(); |
05 |
conn.connect(); |
06 |
// 獲取流 |
07 |
InputStream is = conn.getInputStream(); |
08 |
// 將圖像留轉換成實體Bitmap對象 |
09 |
Bitmap bm = BitmapFactory.decodeStream(is); //關鍵所在
|
10 |
// 關閉連接 |
11 |
is.close(); |
12 |
imageView.setImageBitmap(bm); |
最後更新:2017-04-02 06:51:46