Android读取网络图片 [复制链接]

2012-8-28 13:43
bayou 阅读:3477 评论:0 赞:3
Tag:  图片 网络 Android
Java代码

[代码]java代码:

/**
	 * 读取网络图片
	 * 
	 * @author ysh
	 * 
	 */
	public static Bitmap getBitmap(String url) {
		Bitmap bt = null;
		if (url != null && !url.equals("")) {
			InputStream is = null;
			try {
				URL url1 = new URL(url);
				HttpURLConnection conn = (HttpURLConnection) url1
						.openConnection();

				conn.setDoInput(true);
				conn.connect();
				is = conn.getInputStream();
				bt = BitmapFactory.decodeStream(is);
				is.close();
				conn.disconnect();

			} catch (MalformedURLException e) {
				e.printStackTrace();
				return bt;
			} catch (IOException e) {
				e.printStackTrace();
				return bt;
			}

		}

		return bt;

	}

private Bitmap mBitmap;


try {
	json = HttpDownload.getJSONData(url);
	JSONObject root;
	try {
		root = new JSONObject(json.toString());
		String items = root.getString("ImageUrl");// 获取服务端json中数据
		Integer count = Integer.parseInt(root.getString("Count"));
		if (count == 0) {
			mBitmap = BitmapFactory.decodeFile(filePath_old);
		} else {
			Log.i("======= :", "" + items);
			String img_url = items;
			String fileName = img_url.substring(
					img_url.lastIndexOf('/') + 1, img_url.length());// 提取下载图片的文件名
			mBitmap = Commons.getBitmap(img_url);
			DeleteFile(filePath_old);
			saveFile(mBitmap, fileName);
		}
	} catch (JSONException e) {
		e.printStackTrace();
	}
} catch (ClientProtocolException e) {
	e.printStackTrace();
} catch (IOException e) {
	e.printStackTrace();
}

Drawable draw = new BitmapDrawable(mBitmap);
imgView.setBackgroundDrawable(draw);


我来说两句
您需要登录后才可以评论 登录 | 立即注册
facelist
所有评论(0)
领先的中文移动开发者社区
18620764416
7*24全天服务
意见反馈:1294855032@qq.com

扫一扫关注我们

Powered by Discuz! X3.2© 2001-2019 Comsenz Inc.( 粤ICP备15117877号 )