摘要:Android - 顯示網路圖片
Android要顯示網路圖片。
首先,
要建立ImageView
然後,撰寫抓取網路圖片的函式如下
public class ImgUtils {
//讀取網路圖片,型態為Bitmap
public static Bitmap getBitmapFromURL(String imageUrl){
try
{
URL url = new URL(imageUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap bitmap = BitmapFactory.decodeStream(input);
return bitmap;
}
catch (IOException e)
{
e.printStackTrace();
return null;
}
}
}
取ImageView
ImageView img = (ImageView)view.findViewById(R.id.img);
設定image
img.setImageBitmap(ImgUtilsgetBitmapFromURL(image_url));