Android 网络下载图片

2中方法:

1.

public byte[] downloadResource(Context context, String url)
			throws ClientProtocolException, IOException {
		isStop = false;
		ByteArrayBuffer buffer = null;
		HttpGet hp = new HttpGet(url);
		httpClient = new DefaultHttpClient();
		String netType = isNetType(context);
		if (netType != null & netType.equals("cmwap")) {
			HttpHost proxy = new HttpHost("10.0.0.172", 80);
			httpClient.getParams().setParameter(ConnRouteParams.DEFAULT_PROXY,
					proxy);
		}
		HttpConnectionParams.setConnectionTimeout(httpClient.getParams(),
				5 * 1000);
		HttpConnectionParams.setSoTimeout(httpClient.getParams(), 60 * 1000);
		
		HttpResponse response = httpClient.execute(hp);
		if (response.getStatusLine().getStatusCode() == 200) {
			inputstream = response.getEntity().getContent();
			if (inputstream != null) {
				int i = (int) response.getEntity().getContentLength();
				buffer = new ByteArrayBuffer(1024);
				byte[] tmp = new byte[1024];
				int len;
				while (((len = inputstream.read(tmp)) != -1)
						&& (false == isStop)) {
					buffer.append(tmp, 0, len);
				}
			}
			cancel();
		}
		return buffer.toByteArray();
	}
调用方法:

protected Bitmap doInBackground(WonderfulprogramInfo... params)
		{
			Bitmap bitmap = null;
			try
			{
				String urls = Constant.url + params[0].getWonderfulImgUrl();
				boolean isExists = Files.compare(urls);
				if (isExists == false)
				{	//网络下载图片数据
					Net net = new Net();
					byte[] data = net.downloadResource(HomeActivity.this, urls);
					bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
					imagesCache.put(urls, bitmap); // 把下载好的图片保存到缓存中
					Files.saveImage(urls, data);
				} else
				{	//本地读取图片数据
					byte[] data = Files.readImage(urls);
					BitmapFactory.Options opt = new BitmapFactory.Options();
					opt.inPreferredConfig = Bitmap.Config.RGB_565;
					opt.inPurgeable = true;
					opt.inInputShareable = true;
					// //获取资源图片
					// InputStream is =
					// context.getResources().openRawResource(resId);
					// return BitmapFactory.decodeStream(is,null,opt);

					bitmap = BitmapFactory.decodeByteArray(data, 0, data.length, opt);
					imagesCache.put(urls, bitmap); // 把下载好的图片保存到缓存中
				}

方法二:

class DownLoadTask extends AsyncTask<String, Void, Bitmap> {
		private ImageView imageView;
		private Integer positions;

		public DownLoadTask(ImageView view, int position) {
			imageView = view;
			this.positions = position;
		}

		protected Bitmap doInBackground(String... params) {
			URL url;
			try {
				url = new URL(params[0]);
				InputStream is = url.openStream();
				BitmapFactory.Options opt = new BitmapFactory.Options();
				opt.inPreferredConfig = Bitmap.Config.RGB_565;
				opt.inPurgeable = true;
				opt.inInputShareable = true;
				// 获取资源图片
				Bitmap bitmap = BitmapFactory.decodeStream(is, null, opt);
				HomeActivity.TopGalleryBitmap.put(positions, bitmap);
				return bitmap;
			} catch (OutOfMemoryError err) {
				err.printStackTrace();
			} catch (MalformedURLException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}

			return null;
		}

		protected void onPostExecute(Bitmap result) {
			super.onPostExecute(result);
			imageView.setImageBitmap(result);
		}
	}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值