从服务器读取图片和显示图片的代码如下:
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.image);
iView=(ImageView)findViewById(R.id.image_imageView1);
Bitmap bitmap=getBitMap();
//BitmapDrawable bmd=new BitmapDrawable(bitmap);
//iView.setImageDrawable(bmd);
iView.setImageBitmap(bitmap);//用BitmapDrawable实现也行
}
private ImageView iView;
public Bitmap getBitMap(){
String imageUrl="http://172.16.5.36:8080/AndroidConnectionTest/image/g.png";
Bitmap bitmap=null;
URL url=null;
try {
url=new URL(imageUrl);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(url!=null){
try {
HttpURLConnection huc=(HttpURLConnection) url.openConnection();
huc.setDoInput(true);
huc.connect();
InputStream in=huc.getInputStream();
bitmap=BitmapFactory.decodeStream(in);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return bitmap;
}