本文主要根据代码实例来分析安卓客户端获取网络的图片。
1、获取image的通用方法,根据URL地址参数来获得
public class ImageService {
public static Bitmap getImage(String path) throws Exception{
URL url = new URL(path) ;
HttpURLConnection conn = (HttpURLConnection) url.openConnection() ;
conn.setConnectTimeout(5000);
conn.setRequestMethod("GET");
if(conn.getResponseCode() == 200){
InputStream inStream = conn.getInputStream() ;
Bitmap bm = BitmapFactory.decodeStream(inStream) ;
return bm ;
}
return null;
}
}
2、该方法的变量可以根据具体内容进行修改
public void showimage(ImageView v){
et为文本框内的URL地址
String path = et.getText().toString() ;
try {
Bitmap bm = ImageService.getImage(path) ;
v.setImageBitmap(bm);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(getApplicationContext(), "this is wrong.", 1).show();
}
}
3、然后调用步骤2的方法即可
iv:即ImageView控件
showimage(iv);