显示进度条直到下载完成 - Android

时间:2018-06-01 12:20:51

标签: java android

我使用以下代码从互联网上下载视频:

class DownloadFile1 extends AsyncTask<String, Integer, String> {

    public String videoToDownload;
    public String fileName;

    @Override
    protected String doInBackground(String... params) {
        int count;

        try {
            mp4load(videoToDownload);
        } catch (Exception e) {
            // TODO: handle exception
        }

        return null;
    }

    public void mp4load(String urling) {
        try {
            System.out.println("Downloading");
            URL url = new URL(urling);
            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            con.setRequestMethod("GET");
            //c.setDoOutput(true);
            con.connect();

            // String downloadsPath = Environment.getExternalStoragePublicDirectory();
            File SDCardRoot = Environment.getExternalStorageDirectory();

            File outputFile = new File(SDCardRoot, fileName);

            if (!outputFile.exists()) {
                outputFile.createNewFile();
            }

            FileOutputStream fos = new FileOutputStream(outputFile);

            int status = con.getResponseCode();

            InputStream is = con.getInputStream();

            byte[] buffer = new byte[1024];
            int len1 = 0;
            while ((len1 = is.read(buffer)) != -1) {
                fos.write(buffer, 0, len1);
            }
            fos.close();
            is.close();
            System.out.println("Downloaded");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

我想在视频下载之前添加进度条。进度条应该从下载开始到下载结束以任何格式显示(即)它可以是循环进度条等。怎么做?

我有一个代码可以在异步任务中添加进度条。以下代码是否正确?

                        @Override
                        protected void onPostExecute(Void result) {
                            bar.dismiss();
                            super.onPostExecute(result);
                        }
                        @Override
                        protected void onPreExecute() {
                            bar = new ProgressDialog(activity);
                            bar.setMessage("Processing...");
                            bar.setIndeterminate(true);
                            super.onPreExecute();
                        }

4 个答案:

答案 0 :(得分:0)

更改您的代码,如下所示:

class DownloadFile1 extends AsyncTask<String, Integer, String> {
    ProgressDialog bar;
    public String videoToDownload;
    public String fileName;
    private Context mContext;

    public DownloadFile1(Context context)
     {
      mContext=context;  
     }
    @Override
    protected void onPreExecute()
    {
       super.onPreExecute();
       bar = new ProgressDialog(mContext);
       bar.setMessage("Processing...");
       bar.setIndeterminate(true);
    }

    @Override
    protected String doInBackground(String... params) {
        int count;

        try {
            mp4load(videoToDownload);
        } catch (Exception e) {
            // TODO: handle exception
        }

        return null;
    }

    @Override
    protected String onPostExecute()
    {
      super.onPostExecute(result);
      bar.dismiss();
    }

    public void mp4load(String urling) {
        try {
            System.out.println("Downloading");
            URL url = new URL(urling);
            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            con.setRequestMethod("GET");
            //c.setDoOutput(true);
            con.connect();

            // String downloadsPath = Environment.getExternalStoragePublicDirectory();
            File SDCardRoot = Environment.getExternalStorageDirectory();

            File outputFile = new File(SDCardRoot, fileName);

            if (!outputFile.exists()) {
                outputFile.createNewFile();
            }

            FileOutputStream fos = new FileOutputStream(outputFile);

            int status = con.getResponseCode();

            InputStream is = con.getInputStream();

            byte[] buffer = new byte[1024];
            int len1 = 0;
            while ((len1 = is.read(buffer)) != -1) {
                fos.write(buffer, 0, len1);
            }
            fos.close();
            is.close();
            System.out.println("Downloaded");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

并改变呼叫

DownloadFile1 downloadFile1 = new DownloadFile1(mContext); 

OR

DownloadFile1 downloadFile1 = new DownloadFile1(MainActivity.this); 

然后

downloadFile1.videoToDownload = video_url;
downloadFile1.fileName = video_url;
downloadFile1.execute();

答案 1 :(得分:0)

make progress对话框如下面的代码.. 为显示和隐藏制作两个类似下面的方法。

    private ProgressDialog progressDialog; // it create local level of class.
        progressDialog=new ProgressDialog(MainActivity.this); // this line define into onCreateView method.
    private void showProgress(){
    progressDialog.setMessage("Loading");
    progressDialog.show();
}
private void closeProgress(){
    progressDialog.dismiss();
}

在调用showProgress()方法之前启动dowload,下载完成后再显示closeProgress()。

答案 2 :(得分:0)

Button idview;
TextView nameview, dateview, subjectview, totalview, passview;

下载课程

 /**
     * Showing Dialog
     * */
    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case progress_bar_type: // we set this to 0
            pDialog = new ProgressDialog(this);
            pDialog.setMessage("Downloading file. Please wait...");
            pDialog.setIndeterminate(false);
            pDialog.setMax(100);
            pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            pDialog.setCancelable(true);
            pDialog.show();
            return pDialog;
        default:
            return null;
        }

进度更新

class DownloadFile1 extends AsyncTask<String, Integer, String> {

    public String videoToDownload;
    public String fileName;

    @Override 
    protected String doInBackground(String... params) {
        int count;

        try { 
            mp4load(videoToDownload);
        } catch (Exception e) {
            // TODO: handle exception 
        } 

        return null; 
    } 

    public void mp4load(String urling) {
        try { 
            System.out.println("Downloading");
            URL url = new URL(urling);
            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            con.setRequestMethod("GET");
            //c.setDoOutput(true); 
            con.connect();

            // String downloadsPath = Environment.getExternalStoragePublicDirectory(); 
            File SDCardRoot = Environment.getExternalStorageDirectory();

            File outputFile = new File(SDCardRoot, fileName);

            if (!outputFile.exists()) {
                outputFile.createNewFile();
            } 

            FileOutputStream fos = new FileOutputStream(outputFile);

            int status = con.getResponseCode();

            InputStream is = con.getInputStream();

            byte[] buffer = new byte[1024];
            int len1 = 0;
            while ((len1 = is.read(buffer)) != -1) {
                fos.write(buffer, 0, len1);
            } 
            fos.close();
            is.close();
            System.out.println("Downloaded");
        } catch (IOException e) {
            e.printStackTrace();
        } 
    }

答案 3 :(得分:0)

这是您问题的最佳解决方案,

AndroidNetworking.download(url,dirPath,fileName)
                 .setTag("downloadTest")
                 .setPriority(Priority.MEDIUM)
                 .build()
                 .setDownloadProgressListener(new DownloadProgressListener() {
                    @Override
                    public void onProgress(long bytesDownloaded, long totalBytes) {
                      // do anything with progress  
                    }
                 })
                 .startDownload(new DownloadListener() {
                    @Override
                    public void onDownloadComplete() {
                      // do anything after completion
                    }
                    @Override
                    public void onError(ANError error) {
                      // handle error    
                    }
                });

您可以从以下链接找到此FastNetworking库,

https://github.com/amitshekhariitbhu/Fast-Android-Networking