关于AsyncTask异步下载图片带有进度条更新

首先定义一个ImageView 和一个Button:

<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <ImageView
        android:id="@+id/image"
        android:layout_weight="1000"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" 
        />

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="查看"
        />
    
    
</LinearLayout>

需要添加访问网络权限。在AndroidManifest.xml中添加android.permission.INTERNET权限。

主要代码:
package com.example.http_dowloadview;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectOutputStream.PutField;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.ByteArrayBuffer;
import org.apache.http.util.EntityUtils;

import android.R.integer;
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.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity {

private Button button;
private ImageView image;
String picture = "http://static.statickksmg.com/image/2012/09/26/b120f8a66e5ab2a2c0263e02fba5cb9c.jpg";

private ProgressDialog dia;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.button);
image = (ImageView)findViewById(R.id.image);
//定义进度条
dia = new ProgressDialog(this);
dia.setTitle("提示信息");
dia.setMessage("正在下载、、、");
dia.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
//设置点击进度条外部,不响应;
dia.setCancelable(false);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
new MyTask().execute(picture);
}
});
}
//Sting 表示传入的值, Integer  代表进度 ,  Bitmap 代表返回的值
public class MyTask extends AsyncTask<String, Integer, Bitmap>{

//任务执行之前的准备工作。
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
dia.show();
}
//主要完成耗时操作
@Override
protected Bitmap doInBackground(String... arg0) {
// TODO Auto-generated method stub
Bitmap bitmap = null;
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
InputStream inputStream = null;
try {
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(arg0[0]);
HttpResponse 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;
int value = 0;
while((len = inputStream.read(data)) != -1){
total_length += len;
value = (int)((total_length/(float)file_length)*100);
//调用update函数,更新进度
publishProgress(value);
outputStream.write(data, 0, len);
}
byte[] result = outputStream.toByteArray();
;	 bitmap = BitmapFactory.decodeByteArray(result, 0, result.length);
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally{
if(inputStream != null){
try {
inputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return bitmap;
}
//更新进度条
@Override
protected void onProgressUpdate(Integer... values) {
// TODO Auto-generated method stub
super.onProgressUpdate(values);
dia.setProgress(values[0]);
}
//主要完成耗时操作
@Override
protected void onPostExecute(Bitmap result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
dia.dismiss();
image.setImageBitmap(result);
}
}

}

软件截图:


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值