Downloading multiple files one by one using AsyncTask?

后端 未结 3 1032
囚心锁ツ
囚心锁ツ 2020-12-01 08:46

I\'m trying to download multiple files one by one (file is downloaded, we start downloading the next file). Using this method I can keep track of the files being downloaded.

3条回答
  •  爱一瞬间的悲伤
    2020-12-01 09:09

    package com.ProgressDialogDemo1;
    
    import java.io.BufferedInputStream;
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.net.URL;
    import java.net.URLConnection;
    
    import android.app.Activity;
    import android.app.Dialog;
    import android.app.ProgressDialog;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.os.AsyncTask;
    import android.os.Bundle;
    import android.os.Environment;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.ImageView;
    import android.widget.ProgressBar;
    
    public class ProgressDialogDemoActivity extends Activity {
    
        public static final int DIALOG_DOWNLOAD_PROGRESS = 0;
        private Button startBtn;
        private ProgressDialog mProgressDialog;
        ProgressBar prgBar1;
        ImageView imgv1;
    
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            imgv1 = (ImageView) findViewById(R.id.imgv1);
            startBtn = (Button)findViewById(R.id.startBtn);
            prgBar1 = (ProgressBar) findViewById(R.id.prgBar1);
            prgBar1.setMax(100);
    
            startBtn.setOnClickListener(new OnClickListener(){
                public void onClick(View v) {
                        startDownload();
                }
            });
        }
    
        private void startDownload() {
            String[] url = {"http://animal.discovery.com/birds/peacock/pictures/peacock-picture.jpg",
                        "http://www.funrocker.com/blog/wp-content/uploads/2010/04/Animals-Wild-Life-Jungle-FunRocker.Com-03.jpg",
                        "http://www.thewallpapers.org/photo/5790/Nature_Wallpapers-037.jpg",
                        "http://2.bp.blogspot.com/-j56yaqpfjVE/TnzTjcqnCjI/AAAAAAAAGPM/MzqmczFkC30/s1600/natural-pictures.jpg",
                        "http://www.fantasy-and-art.com/wp-content/gallery/nature-wallpapers/red-tree-wallpaper-hd.jpg",
                        "http://upload.wikimedia.org/wikipedia/commons/thumb/1/1a/Bachalpseeflowers.jpg/300px-Bachalpseeflowers.jpg"};
            DownloadFileAsync dloadFAsync = new DownloadFileAsync(url);
            dloadFAsync.execute(url);
        }
        @Override
        protected Dialog onCreateDialog(int id) {
            switch (id) {
            case DIALOG_DOWNLOAD_PROGRESS:
                mProgressDialog = new ProgressDialog(this);
                mProgressDialog.setCancelable(false);
                return mProgressDialog;
            default:
                return null;
            }
        }
    
        //              Async  Task
        class DownloadFileAsync extends AsyncTask {
            int current=0;
            String[] paths;
            String fpath;
            boolean show = false;
    
            public DownloadFileAsync(String[] paths) {
                super();
                this.paths = paths;
                for(int i=0; i

提交回复
热议问题