安卓中异步任务实现带下载进度的根据图片地址下载图片

点击下载后将图片显示出来:


布局文件:



    
    


逻辑代码文件:

package mars.day7_download;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;


public class DownloadP extends Activity {
	ImageView img;
	DownloadTask downloadtask;
	String imgurl="http://www.guaiwei.com/uploads/allimg/150326/28-150326141503.png";
	ProgressDialog progressdialog;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.download_picture);
		img=(ImageView) findViewById(R.id.img);
	}
	public void download(View v)
	{
		downloadtask=new DownloadTask();
		downloadtask.execute(imgurl);
	}
	public void cancle(View v)
	{
		downloadtask.cancel(true);
	}
	//异步类
	public class DownloadTask extends AsyncTask
	{
		//首次调用加载
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			progressdialog=new ProgressDialog(DownloadP.this);
			progressdialog.setTitle("下载图片");
			progressdialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
			progressdialog.setMessage("正在下载...");
			progressdialog.show();
		}
		//下载
		@Override
		protected byte[] doInBackground(String... params) {
			if(!this.isCancelled())
			{
				//创建请求客户端
				HttpClient hclient=new DefaultHttpClient();
				//请求  imgurl=params[0]
				HttpGet request=new HttpGet(params[0]);
				//字节输出流存放要下载的文件
				ByteArrayOutputStream bo=new ByteArrayOutputStream();
				try 
				{
					//得到客户端执行请求的结果
					HttpResponse response=hclient.execute(request);
					//请求成功执行的操作
					if(response.getStatusLine().getStatusCode()==200)
					{
						//得到请求的实体内容
						InputStream in = response.getEntity().getContent();
						byte b[]=new byte[1024];
						int count=0;
						//得到文件的长度
						long file_length = response.getEntity().getContentLength();
						//每次读取的长度
						int current_length = 0;
						while((count=in.read(b))!=-1)
						{
							//当前下载的长度
							current_length+=count;
							//当前下载进度的百分比
							int progress = (int)((current_length/(float)file_length)*100);
							//发送刻度单位到ui线程,onProgressUpdate里进行更新进度,显示到进度条
							publishProgress(progress);
							bo.write(b,0,count);
							bo.flush();
						}
					}
					//流转换成数组返回
					return bo.toByteArray();
				} 
				catch (Exception e) 
				{
					e.printStackTrace();
				}
				
				
			}
			
			return null;
		}
		//下载完成执行,更新ui
		@Override
		protected void onPostExecute(byte[] result) {
			super.onPostExecute(result);
			//下载的内容传过来后解码放到位图工厂bo.toByteArray()=result
			Bitmap bm = BitmapFactory.decodeByteArray(result, 0, result.length);
			//显示到图片控件
			img.setImageBitmap(bm);
			progressdialog.dismiss();
		}
		//更新进度
		@Override
		protected void onProgressUpdate(Integer... values) {
			super.onProgressUpdate(values);
			//progress=values[0]
			progressdialog.setProgress(values[0]);
		}
		@Override
		protected void onCancelled() {
			super.onCancelled();
			progressdialog.dismiss();
			downloadtask.cancel(true);
		}
		
	}
}



你可能感兴趣的:(安卓,网络)