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);

更多相关文章

  1. 36个Android开发常用代码片段
  2. Android 进阶的小技巧整理(整理自第一行代码)
  3. Android 通过代码设置着色图片
  4. android添加图片到本地,无法即使刷新的解决方法
  5. Android 实现图片加水印
  6. android仿探探图片拖拽功能
  7. android 图片放大缩小
  8. android ImageView实现图片浏览器(点击左侧显示上一张,点击右侧显
  9. android中图片的处理(绘制圆形,圆角||缩略图)

随机推荐

  1. Android 蓝牙设备的开启与关闭功能的实现
  2. android 源码环境下,编译apk时,导入第三方
  3. Bugs with P3100 4.1.2
  4. Music打开出错
  5. android打开,保存图片到sd卡,显示图片
  6. android String资源 包含 数学符号等特殊
  7. Android 自定义Combobox
  8. Android学习教程之日历控件使用(7)
  9. android 悬浮窗口的拖动
  10. Android Activity生命周期简明、详细介绍