标签:java code 路径 news cat name string setimage 几句话
今天写了安卓程序与server通信。当中须要从server获取图片。本来以为下载流、处理文件流非常复杂。结果几句话就轻松搞定了。如今记在这里。
// (2014.5.1第一种方法)通过server返回的图片url,再次向server请求,加入动态新闻图片
// 读取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请求到server的图片路径(不是想象中的图片流,server仅仅会返回请求的图片资源的路径,而后由client依据url再次訪问),然后依据url再次建立连接,请求图片流,并解码解析。生成图片。关联相关的布局文件里的控件,通过setImageBitmap()函数设置资源id就可以。
标签:java code 路径 news cat name string setimage 几句话
原文地址:http://www.cnblogs.com/cynchanpin/p/6790585.html