12月1日备课,下载图片,显示进度

package com.example.progressnotification;

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.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import android.app.Activity;
import android.app.NotificationManager;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

public class MainActivity extends Activity {

private ImageView image;
private int sum;
private boolean isSum = true;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    image = (ImageView) findViewById(R.id.image);
}

public void btnClick(View view) {

    new DownloadImage()
            .execute("http://sony.it168.com/data/attachment/forum/201410/20/222208nct678ofnorcwclf.jpg");

}

class DownloadImage extends AsyncTask<String, Integer, Bitmap> {
    NotificationCompat.Builder builder = new NotificationCompat.Builder(
            MainActivity.this).setSmallIcon(R.drawable.ic_launcher).setContentTitle("下载图片").setContentText("请稍后");
    NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    int totalNum = 0;

    private byte[] imageByteArray;

    @Override
    protected Bitmap doInBackground(String... params) {
        HttpClient client = new DefaultHttpClient();
        HttpGet get = new HttpGet(params[0]);
        ByteArrayOutputStream baos = null;

        try {
            HttpResponse res = client.execute(get);
            if (res.getStatusLine().getStatusCode() == 200) {
                HttpEntity entity = res.getEntity();
                InputStream is = entity.getContent();
                // 获得最大值
                sum = (int) entity.getContentLength();
                publishProgress(sum);
                baos = new ByteArrayOutputStream();
                byte[] b = new byte[100];
                int a = 0;
                int total = 0;
                while ((a = is.read(b)) != -1) {
                    total += a;
                    // 更新当前进度
                    publishProgress(total);
                    baos.write(b, 0, a);
                }
                Bitmap bitmap = BitmapFactory.decodeByteArray(
                        baos.toByteArray(), 0, baos.toByteArray().length);
                return bitmap;
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (baos != null) {
                try {
                    baos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

    @Override
    protected void onPostExecute(Bitmap result) {
        super.onPostExecute(result);
        image.setImageBitmap(result);

    }

    @Override
    protected void onProgressUpdate(Integer... values) {
        super.onProgressUpdate(values);
        if (isSum) {
            totalNum = values[0];
            isSum = false;
        } else {
            builder.setProgress(totalNum,values[0], false);
            manager.notify(1, builder.build());
            if (values[0] == totalNum) {
                builder.setProgress(0, 0, false);
                builder.setContentText("下载完毕");
                manager.notify(1, builder.build());
            }
        }

    }
}

}
这里写图片描述

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值