码迷,mamicode.com
首页 > 其他好文 > 详细

异步操作AsyncTask(二)从网络下载图片(带下载进度数据)

时间:2015-01-28 16:00:48      阅读:224      评论:0      收藏:0      [点我收藏+]

标签:

package com.example.asyntaskproject;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
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.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity {

	private ImageView imageView;
	private Button button;
	//图片的网络地址
	private String image_path = "http://pic4.nipic.com/20090828/2366808_092035042961_2.jpg";
	private ProgressDialog dialog ;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		imageView = (ImageView) findViewById(R.id.imageView1);
		button = (Button) findViewById(R.id.button1);
		dialog = new ProgressDialog(this);
		dialog.setTitle("提示");
		dialog.setMessage("正在下载,请稍后..");
		dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
		dialog.setCancelable(false);
		//不让屏幕失去焦点,即点其他的地方的时候进度条不消失
		button.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				new MyTask().execute(image_path);
			}
		});
	}
	public class MyTask extends AsyncTask<String, Integer, Bitmap>{

		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
			dialog.show();
		}
		@Override
	        protected void onProgressUpdate(Integer... values) {
			// TODO Auto-generated method stub
			super.onProgressUpdate(values);
			dialog.setProgress(values[0]);
		}
		@Override
		protected Bitmap doInBackground(String... params) {
			// TODO Auto-generated method stub
			Bitmap bitmap = null;
			//声明一个内存流,用于将他图片读取放到内存缓冲区中
			ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
			InputStream inputStream = null;
			HttpClient httpClient = new DefaultHttpClient();
			HttpGet httpGet = new HttpGet(params[0]);
			HttpResponse httpResponse;
			try {
				httpResponse = httpClient.execute(httpGet);
				if(httpResponse.getStatusLine().getStatusCode() == 200){
					inputStream = httpResponse.getEntity().getContent();
					long file_length = httpResponse.getEntity().getContentLength();
					int len = 0;
					byte[] data = new byte[1024];
					int total_length = 0;
					while((len = inputStream.read(data)) != -1){
						total_length += len;
						//刻度百分比
						int value = (int)((total_length/(float)file_length)*100);
						publishProgress(value);
						outputStream.write(data, 0, len);
					}
					byte[] result = outputStream.toByteArray();
					bitmap = BitmapFactory.decodeByteArray(result, 0, result.length);
				}
			}catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}finally{
				if(inputStream != null){
					try {
						inputStream.close();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
			return bitmap;
		}
		@Override
		protected void onPostExecute(Bitmap result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			dialog.dismiss();
			imageView.setImageBitmap(result);
		}
	}
}

技术分享技术分享技术分享

免积分源代码下载

异步操作AsyncTask(二)从网络下载图片(带下载进度数据)

标签:

原文地址:http://blog.csdn.net/lindonglian/article/details/43231487

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!