使用AsyncTask实现异步文件下载

在android中使用异步的方式很多 比如:handler、runOnUIThread、Loopper、AsyncTask,现在用到了asyncTask来下载文件,故录之

用一个类继承AsyncTask,其中有三个参数需要明确:

params:外部传入的参数 比如URL

Progress:显示进度刻度

Result:请求完成之后返回的结果

AsyncTask<Params, Progress, Result>

使用AsyncTask下载网络图片

class AsyncTaskClass extends AsyncTask<String, Integer, Bitmap>{
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			dialog.show();
		}
		@Override
		protected Bitmap doInBackground(String... params) {
			HttpClient httpClient=new DefaultHttpClient();
			HttpGet get=new HttpGet(params[0]);
			Bitmap bitmap=null;
			try {
				HttpResponse httpResponse=httpClient.execute(get);
				if(httpResponse.getStatusLine().getStatusCode()==200){
					HttpEntity entity=httpResponse.getEntity();
					byte[]data=EntityUtils.toByteArray(entity);
					bitmap=BitmapFactory.decodeByteArray(data, 0, data.length);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			return bitmap;
		}
		@Override
		protected void onPostExecute(Bitmap result) {
			super.onPostExecute(result);
			asynciv.setImageBitmap(result);
			dialog.dismiss();
		}
		@Override
		protected void onProgressUpdate(Integer... values) {
			super.onProgressUpdate(values);
		}
	}
使用AsyncTask下载网络图片并显示进度条

class AsyncTaskProgressDialog extends AsyncTask<String, Integer, Bitmap>{
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			dialog.show();
		}
		
		@Override
		protected Bitmap doInBackground(String... params) {
			HttpClient client=new DefaultHttpClient();
			HttpGet httpGet=new HttpGet(params[0]);
			Bitmap bitmap=null;
			ByteArrayOutputStream baos=new ByteArrayOutputStream();
			InputStream is=null;
			try {
				HttpResponse httpResponse=client.execute(httpGet);
				if(httpResponse.getStatusLine().getStatusCode()==200){
					is=httpResponse.getEntity().getContent();
					long file_length=httpResponse.getEntity().getContentLength();
					int len=0;
					byte[] data=new byte[1024];
					int total_lengh=0;
					while((len=is.read(data))!=-1){
						total_lengh+=len;
						int value=(int) ((total_lengh / (float) file_length) * 100);
						publishProgress(value);
						baos.write(data, 0, len);
					}
					byte []result=baos.toByteArray();
					bitmap=BitmapFactory.decodeByteArray(result, 0, result.length);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}finally{
				try {
					baos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			return bitmap;
		}
		@Override
		protected void onProgressUpdate(Integer... values) {
			super.onProgressUpdate(values);
			dialog.setProgress(values[0]);
		}
		@Override
		protected void onPostExecute(Bitmap result) {
			super.onPostExecute(result);
			asynciv.setImageBitmap(result);
			dialog.dismiss();
		}
	}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值