下面来看一个具体例子,从网上下载一张图片并显示:
1、创建DownloadPicture类继承AsyncTask:
package com.example.test02;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import Android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.util.Log;
public class DownloadPicture extends AsyncTask
{
private DownloadCallBack downloadCallBack;
public DownloadPicture(DownloadCallBack downloadCallBack )
{
this.downloadCallBack = downloadCallBack;
}
//创建一个回调接口,用于在子线程执行时调回主线程,执行自定义操作
public interface DownloadCallBack
{
public void DoInPre();
public void Doing(int length , int max);
public void DoInFinish(Bitmap bitmap);
}
@Override
protected void onPreExecute()
{
downloadCallBack.DoInPre();
super.onPreExecute();
}
@Override
protected Bitmap doInBackground(String... params)
{
try
{
URL url = new URL(params[0].toString());
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setConnectTimeout(5000);
int max = connection.getContentLength();
InputStream inputStream = connection.getInputStream();
ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int length = 0;
int total = 0;
while ((length = inputStream.read(buffer)) != -1)
{
arrayOutputStream.write(buffer, 0, length);
total += length;
//发送数据,更新下载进度
publishProgress(total , max);
Thread.sleep(100);
}
byte[] result = arrayOutputStream.toByteArray();
return BitmapFactory.decodeByteArray(result, 0, result.length);
}
catch (Exception e)
{
e.printStackTrace();
}
return null;
}
@Override
protected void onProgressUpdate(Integer... values)
{
Log.i("输出", "onProgressUpdate..." + values[0]);
downloadCallBack.Doing(values[0] , values[1]);
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(Bitmap result)
{
downloadCallBack.DoInFinish(result);
super.onPostExecute(result);
}
@Override
protected void onCancelled()
{
downloadCallBack.DoInFinish(null);
super.onCancelled();
}
}
2、实例化DownloadPicture,并执行:
downloadPicture = new DownloadPicture(new downloadCallBack());
downloadPicture.execute("upload/2014_05/140526122650482.jpg");
3、编辑匿名内部类downloadCallBack:
private class downloadCallBack implements DownloadCallBack
{
public void DoInPre()
{
dialog = new ProgressDialog(MainActivity.this);
dialog.setButton("取消", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface arg0, int arg1)
{
downloadPicture.cancel(true);
dialog.dismiss();
}
});
dialog.setCancelable(true);
dialog.setTitle("正在下载...");
dialog.setMessage("正在下载图片...");
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
dialog.show();
}
public void Doing(int length , int max)
{
Log.i("输出", "Doing..." + length);
dialog.setMessage((int)((length/(float)max)*100) + "");
dialog.setProgress((int)((length/(float)max)*100));
}
public void DoInFinish(Bitmap bitmap)
{
dialog.dismiss();
if (bitmap != null)
{
imageView.setImageBitmap(bitmap);
}
}
大功告成,运行即可,下面是截图