Android从服务器获取图片的实例方法
16lz
2021-01-23
Android从服务器获取图片的实例方法,需要的朋友可以参考一下
代码如下:
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是你的图片路径,
最后可以将图片显示在手机上:
代码如下:
imageView.setImageBitmap(bitmap);更多相关文章
- 36个Android开发常用代码片段
- Android 进阶的小技巧整理(整理自第一行代码)
- Android 通过代码设置着色图片
- android添加图片到本地,无法即使刷新的解决方法
- Android 实现图片加水印
- android仿探探图片拖拽功能
- android 图片放大缩小
- android ImageView实现图片浏览器(点击左侧显示上一张,点击右侧显
- android中图片的处理(绘制圆形,圆角||缩略图)