android 网络加载图片

在高级的android 版本上,加载图片
	/**
	 * 从网络中获取图片,以流的形式返回
	 * 
	 * @return
	 */
	public static InputStream getImageViewInputStream(String URL_PATH)
			throws IOException {
		InputStream inputStream = null;
		URL url = new URL(URL_PATH); // 服务器地址
		if (url != null) {
			// 打开连接
			HttpURLConnection httpURLConnection = (HttpURLConnection) url
					.openConnection();
			httpURLConnection.setConnectTimeout(3000);// 设置网络连接超时的时间为3秒
			httpURLConnection.setRequestMethod("GET"); // 设置请求方法为GET
			httpURLConnection.setDoInput(true); // 打开输入流
			// int responseCode = httpURLConnection.getResponseCode(); //
			// 获取服务器响应值
			// if (responseCode == HttpURLConnection.HTTP_OK) { //正常连接
			inputStream = httpURLConnection.getInputStream(); // 获取输入流
			// }
		}
		return inputStream;
	}

该函数必须要在新开的一个线程中调用,否则报错

调用代码

		new Thread(new Runnable() {
			@Override
			public void run() {
				InputStream inputStream = null;
				try {
					inputStream = getImageViewInputStream(NetWorkApiContext.URL_TEMP
							+ imgL[i]);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		        BitmapFactory.Options opt = new BitmapFactory.Options();

		        opt.inPreferredConfig = Bitmap.Config.RGB_565;

		        opt.inPurgeable = true;

		        opt.inInputShareable = true;
				bitmap = BitmapFactory.decodeStream(inputStream,null,opt);
				Message msg=new Message();
				msg.what=MSG_FINISH;
				msg.obj=bitmap;
				handle.sendMessage(msg);
			}

		}).start();


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值