显示下载进度条

Posted

技术标签:

【中文标题】显示下载进度条【英文标题】:show download progress bar 【发布时间】:2021-10-26 04:24:50 【问题描述】:

在我的应用程序中,我使用执行器服务从 url 下载文件。现在我想添加一个水平进度条来显示下载进度。但我面临错误。如何在不使用异步任务的情况下在此代码中添加进度条?

 private class ExecutorServiceDownload implements Runnable 
        private String url;

        public ExecutorServiceDownload(String url) 
            this.url = url;
        

        @Override
        public void run() 
            dlFile(url);
        

        private String dlFile(String surl) 


            try 

// I added progressbar here and it didn't download anything 


                InputStream input = null;
                OutputStream output = null;
                HttpURLConnection connection = null;

                URL url = new URL(surl);
                        connection = (HttpURLConnection) url.openConnection();
                        connection.connect();

                        if (connection.getResponseCode() != HttpURLConnection.HTTP_OK)
                            return "Server returned HTTP " + connection.getResponseCode() + " " + connection.getResponseMessage();

                        input = connection.getInputStream();
                        String title = URLUtil.guessFileName(String.valueOf(url), null, null);
                        output = new FileOutputStream(Environment.getExternalStorageDirectory().toString() + "/test_files" + "/" + title);
                        int contentLength = connection.getContentLength();

                        byte data[] = new byte[4096];
                        long total = 0;
                        int count;
                        while ((count = input.read(data)) != -1) 
                            total += count;
                            output.write(data, 0, count);
                        
                 catch (Exception e) 
                    return e.toString();
                
           return null;
        
    

【问题讨论】:

***.com/questions/3028306/… 我以前看过这个。我不想使用异步任务 【参考方案1】:

将此代码与执行器服务一起下载:

在 onCreate 中:

ProgressBar progressBar;

@Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreat`enter code here`e(savedInstanceState);
        setContentView(R.layout.activity_main);
        progressBar = findViewById(R.id.progressbar);

    
    downloading();

使用执行器服务下载文件:

private void downloading() 
progressBar.setVisibility(View.VISIBLE);


ExecutorService executor = Executors.newSingleThreadScheduledExecutor();
Handler handler = new Handler(Looper.getMainLooper());

executor.execute(new Runnable() 

    int count;

    @Override
    public void run() 

        //Background work here
        try 

            // put your url.this is sample url.
            URL url = new URL("http://techslides.com/demos/sample-videos/small.mp4");
            URLConnection conection = url.openConnection();
            conection.connect();

      

            int lenghtOfFile = conection.getContentLength();

            // download the file

            InputStream input = conection.getInputStream();

            //catalogfile is your destenition folder
            OutputStream output = new FileOutputStream(catalogfile + "video.mp4");


            byte data[] = new byte[1024];

            long total = 0;

            while ((count = input.read(data)) != -1) 
                total += count;
                // publishing the progress....


                publishProgress(Integer.valueOf("" + (int) ((total * 100) / lenghtOfFile)));

                // writing data to file
                output.write(data, 0, count);
            

            // flushing output
            output.flush();

            // closing streams
            output.close();
            input.close();


            handler.post(new Runnable() 
                @Override
                public void run() 
                    //UI Thread work here
                    progressBar.setVisibility(View.GONE);

                
            );
         catch (Exception e) 

        
    
);

更新进度条

    private void publishProgress(Integer... progress) 

     progressBar.setProgress(progress[0]);

    

和水平进度条的使用:

 <ProgressBar
    android:max="100"
    style="?android:attr/progressBarStyleHorizontal"
    android:visibility="invisible"
    android:id="@+id/progressbar"
    android:layout_
    android:layout_

【讨论】:

以上是关于显示下载进度条的主要内容,如果未能解决你的问题,请参考以下文章

关于VB.NET中进度条使用问题

下载文件时进度条不显示剩余百分比?

VC下载文件显示进度条

Python 给下载文件显示进度条和下载时间

赵雅智_android多线程下载带进度条

Python获取下载速度并显示进度条