Android实现下载文件(图片)显示进度

这里是做笔记用,不做过多介绍:

1、布局代码:download.xml




    

    

    

2、接下来是Activity代码:

package com.example.weiwenyi.androidtest;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.SystemClock;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.example.weiwenyi.androidtest.dowmDemo.iview.DownloadListener;
import com.example.weiwenyi.androidtest.dowmDemo.util.DownloadTask;
import com.example.weiwenyi.androidtest.util.Util;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class DownLoadActivity extends AppCompatActivity implements View.OnClickListener, DownloadListener {

    ProgressBar progressBar;
    Button btnStartLoad;
    TextView bf;
    ImageView image;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.download);

        progressBar = findViewById(R.id.progress);
        btnStartLoad = findViewById(R.id.btnStartLoad);
        bf = findViewById(R.id.bf);
        image = findViewById(R.id.image);

        btnStartLoad.setOnClickListener(this);
    }
    DownImageTask downLoadAsynctask;
    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btnStartLoad){
            downLoadAsynctask = new DownImageTask();
            downLoadAsynctask.execute("http://img04.muzhiwan.com/2015/06/16/upload_557fd293326f5.jpg");

        }

    }
    String url = "http://pic4.nipic.com/20091217/3885730_124701000519_2.jpg";

    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    public void onProgress(int progress) {
        progressBar.setProgress(progress,true);
    }

    @Override
    public void onSuccess() {
        Util.showToast("下载完成");
    }

    @Override
    public void onFailed() {
        Util.showToast("下载失败");
    }

    @Override
    public void onPaused() {
        Util.showToast("暂停下载");
    }

    @Override
    public void onCanceled() {
        Util.showToast("关闭下载");
    }


    class DownImageTask  extends AsyncTask {
        // 执行预处理
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            // 显示进度条

        }
        // 后台进程的执行
        @Override
        protected Bitmap doInBackground(String... params) {

            Bitmap   bitmap=null;
            try {
                URL url = new URL(params[0]);
                HttpURLConnection conn = (HttpURLConnection) url
                        .openConnection();
                // 进度条的更新,我这边只是用一个循环来示范,在实际应用中要使用已下载文件的大小和文件总大小的比例来更新
                for (int i = 1; i <= 10; i++) {
                    publishProgress(i * 10);
                    Thread.sleep(200);
                }
                InputStream inputStream = conn.getInputStream();
                bitmap = BitmapFactory.decodeStream(inputStream);
                inputStream.close();
                Log.i("图片","--1---->  " + bitmap);
                image.setImageBitmap(bitmap);

            } catch (Exception e) {
                e.printStackTrace();
//                msg= e.getMessage();
            }
            return bitmap;
        }
        // 运行于UI线程,对后台任务的结果做出处理,doInBackground方法执行的结果作为此方法的参数
        @Override
        protected void onPostExecute(Bitmap result) {
            super.onPostExecute(result);
            Log.i("图片","---2--->  " + result);
            image.setImageBitmap(result);

        }
        // 运行于UI线程,如果在doInBackground(Params...)中使用了publishProgress(Progress...),就会触发此方法
        @Override
        protected void onProgressUpdate(Integer... values) {
            super.onProgressUpdate(values);
            bf.setText( ""+ values[0] +"%" );
            progressBar.setProgress(values[0]);

        }
    }
}

运行实现点击下载,显示进度,下载完并在布局下方显示下载的图片

你可能感兴趣的:(Android开发)