从服务器获取图片 Android从服务器获取图片的实例方法
人气:0想了解Android从服务器获取图片的实例方法的相关内容吗,在本文为您仔细讲解从服务器获取图片的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:服务器,获取,图片,下面大家一起来学习吧。
[java]
复制代码 代码如下:
public static Bitmap getBitmapFromServer(String imagePath) {
HttpGet get = new HttpGet(imagePath);
HttpClient client = new DefaultHttpClient();
Bitmap pic = null;
try {
HttpResponse response = client.execute(get);
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
pic = BitmapFactory.decodeStream(is); // 关键是这句代码
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return pic;
}
public static Bitmap getBitmapFromServer(String imagePath) {
HttpGet get = new HttpGet(imagePath);
HttpClient client = new DefaultHttpClient();
Bitmap pic = null;
try {
HttpResponse response = client.execute(get);
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
pic = BitmapFactory.decodeStream(is); // 关键是这句代码
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return pic;
}
其中imagePath是你的图片路径,
最后可以将图片显示在手机上:
[java]
复制代码 代码如下:
imageView.setImageBitmap(bitmap);
加载全部内容