<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity" > <ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher" android:maxHeight="300dp" android:maxWidth="300dp" android:adjustViewBounds="true"/> <Button android:id="@+id/btn_download" android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="onDownLoadImg" android:text="点击下载图片" android:textSize = "20sp"/> </LinearLayout>
main.java
package com.example.day07_downloadimg_prgress; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import android.os.AsyncTask; import android.os.Bundle; import android.app.Activity; import android.app.ProgressDialog; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.view.Menu; import android.view.View; import android.widget.ImageView; import android.widget.Toast; /** * //弹出进度条对话框基本使用 //创建进度条对话框 ProgressDialog progressDialog = new ProgressDialog(MainActivity.this); //设置标题 progressDialog.setTitle("正在下载"); //设置内容 progressDialog.setMessage("下载中"); //设置弹出对话框显示的样式 progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); //显示 progressDialog.show(); //给进度条设置值,在show()之后设置 progressDialog.setProgress(50); //隐藏 progressDialog.hide(); * * */ /**注意:需要在清单文件中添加网络权限 * 1.初始化控件,数据 * 2.设置下载点击事件 * 2.1.开启异步任务下载图片 * 1.先显示对话框,进度为0 * 2.开始下载 * 1.获得文件大小 * 2.获得当前下载的总量 * 3.计算当前百分比 * 4.通知对话框更新更改进度(主线程) * 5.下载完成,返回数据 * 3.下载完成 * 1.关闭对话框 * 2.显示下载成功的图片 * @author my * */ public class MainActivity extends Activity { private ImageView image; private static final String imgPath = "http://c.hiphotos.baidu.com/image/h%3D200/sign=8cbc53a04ded2e73e3e9812cb700a16d/f7246b600c338744513e9358560fd9f9d72aa01f.jpg"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); image = (ImageView) findViewById(R.id.image); } public void onDownLoadImg(View v){ new MyAsyncTask().execute(imgPath); } class MyAsyncTask extends AsyncTask<String, Integer, Bitmap>{ private ProgressDialog progressDialog; @Override protected void onPreExecute() { super.onPreExecute(); progressDialog = new ProgressDialog(MainActivity.this); progressDialog.setTitle("正在下载"); progressDialog.setMessage("下载中..."); progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); progressDialog.show(); } @Override protected Bitmap doInBackground(String... params) { try { DefaultHttpClient httpClient = new DefaultHttpClient(); HttpGet httpGet = new HttpGet(params[0]); HttpResponse response = httpClient.execute(httpGet); if(200 == response.getStatusLine().getStatusCode()){ //获得实体对象 HttpEntity entity = response.getEntity(); //得到输入流 InputStream is = entity.getContent(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); //获得要下载图片的总长度 long contentLength = entity.getContentLength(); //当前下载图片的长度 int currentLength = 0; byte[] buf = new byte[50]; int len = 0; while(-1 !=(len = is.read(buf))){ currentLength += len; //计算当前的百分比 int percent =(int) ((currentLength/(float)contentLength)*100); //通知调用主线程的方法更新进度,自动调用onProgressUpdate()方法 publishProgress(percent); //写到流中 baos.write(buf, 0, len); baos.flush(); } is.close(); baos.close(); //把字节流转换成字节数组 byte[] byteArray = baos.toByteArray(); //把字节流转换成Bitmap Bitmap bitmap = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length); return bitmap; } } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } @Override protected void onPostExecute(Bitmap result) { super.onPostExecute(result); //关闭对话框 progressDialog.dismiss(); if(result != null){ image.setImageBitmap(result); }else{ Toast.makeText(MainActivity.this, "网络错误", 0).show(); } } //在主线程中执行,参数类型与类定义泛型的第二个参数一致 @Override protected void onProgressUpdate(Integer... values) { super.onProgressUpdate(values); //更新进度 progressDialog.setProgress(values[0]); } } }