如何从服务器获取图片
2014-05-08 22:16
183 查看
今天写了安卓程序与服务器通信,其中需要从服务器获取图片。本来以为下载流、处理文件流很复杂,结果几句话就轻松搞定了。现在记在这里。
// (2014.5.1第一种方法)通过服务器返回的图片url,再次向服务器请求,添加动态新闻图片 // 读取Bitmap图片 try { Bitmap bm; URL url; url = new URL(map.get("activityPhoto").toString()); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); InputStream is = conn.getInputStream(); bm = BitmapFactory.decodeStream(is); // 加载到布局文件中 newsImage = (ImageView) findViewById(R.id.imageView); newsImage.setImageBitmap(bm); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }主要过程是先通过Ajax请求到服务器的图片路径(不是想象中的图片流,服务器只会返回请求的图片资源的路径,而后由客户端根据url再次访问),然后根据url再次建立连接,请求图片流,并解码解析,生成图片。关联相关的布局文件中的控件,通过setImageBitmap()函数设置资源id即可。
相关文章推荐
- Android如何从服务器获取图片
- Android如何从服务器获取图片
- AFNetworking如何向服务器提交图片 ios中获取图片并上传图片
- Android如何从服务器获取图片
- ASP中如何获取服务器的名称
- 如何通过js和jquery获取图片真实的宽度和高度
- 如何用 PHP 来获取客户端 IP 和 服务器(主机)的 IP
- Delphi如何获取图片中第一个像素的颜色值
- android从本地媒体库获取图片上传至服务器保存
- Android 如何通过相册和相机来获取图片
- 如何获取手机内存视频(图片)略缩图
- 如何将含有大量图片或公式的word文档上传到web服务器
- js和jquery如何获取(图片)真实的宽度和高度
- 安卓获取服务器返回的图片资源路径并下载图片
- Gallery图片库获取到图片之后如何通过Uri获取到片图路径
- 如何获取图片的SVG的数据
- 如何获取drawable目录下的图片绝对路径
- ios中摄像头/相册获取图片,压缩图片,上传服务器方法总结
- ios中摄像头/相册获取图片,压缩图片,上传服务器方法总结
- 如何从ipa包中获取png图片