显示下载进度条

     2023-05-07     89

关键词:

【中文标题】显示下载进度条【英文标题】: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_

【讨论】:

vc下载文件显示进度条

VC下载文件显示进度条 逗比汪星人2009-09-18上传 byKomahttp://blog.csd.net/wangningyuhttp://download.csdn.net/detail/wangningyu/1674247 查看详情

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

】下载文件时进度条不显示剩余百分比?【英文标题】:progressbarnotshowremainingbypercentwhendownloadfile?【发布时间】:2021-10-2819:28:21【问题描述】:我在Angular8上工作,我遇到问题进度条不显示部分下载假设我下载数据然后20%然后40%... 查看详情

pythonhttp下载文件并显示下载进度条

...利用request下载文件并写入到文件系统,利用progressbar模块显示下载进度条。其中利用request模块下载文件可以直接下载,不需要使用open方法,例如:import urllibimport requests.packages.urllib3requests.packages.urllib3.disable_warnings()url&n 查看详情

vc下载文件+显示进度条

在codeproject里找了许久,发现这样一个VC下载文件并显示进度条的源码,于是添加了些中文注释: 1、下载线程函数: [cpp] viewplain copy  print?UINT DownloadFile(LPVOID pParam)  {    & 查看详情

ios异步下载下载进度条显示

说到http异步下载,首先要知道其中的关键类。关键类是NSURLConnection  NSURLRequest NSMutableURLRequest 委托是NSURLConnectionDownloadDelegateNSURLConnectionDataDelegateNSURLConnectionDelegate首先,我们要实现最基本的下载功能。LQ 查看详情

python蟒蛇下载大型文件显示进度条(代码片段)

查看详情

从android中的sql server下载表格行时显示进度条

】从android中的sqlserver下载表格行时显示进度条【英文标题】:showprogress-barwhiledownloadingtablerowfromsqlserverinandroid【发布时间】:2017-05-2200:26:06【问题描述】:我正在从android中的sqlserver获取表行,我想在进度条中显示,但我遇到了... 查看详情

iOS,Swift:串行下载多个文件并将所有文件的单个进度条显示为一个进度

...iOS,Swift:串行下载多个文件并将所有文件的单个进度条显示为一个进度【英文标题】:iOS,Swift:Downloadmultiplefileseriallyandshowingsingleprogressbarforallfileasaoneprogress【发布时间】:2020-01-2307:25:12【问题描述】:我正在使用我的ios(swift4)应... 查看详情

[winfrom]下载文件并显示进度条的实现代码(代码片段)

...,可以选择保存路径,点击下载按钮进行下载,下载过程显示下载百分比和进度条。窗体:组件labelTextBoxButtonprogressBarsaveFileDialog获取saveFileDialog1文件名和路径Path.GetDirectoryName(saveFileDialog1.FileName)//获取文件名(不包括路径)P 查看详情

进度条实时显示request下载文件的解决方案

...望和大家一起成长进步。  本文主要介绍了进度条实时显示request下载文件的解决方案,希望对新手有所帮助。文章目录1.背景介绍2.解决方案2.1下载文件解决方案2.2直接解析文件2.2.1解析csv文件2.2.2解析压缩包文件1.背景介绍 ... 查看详情

AsyncTask onPreExecute() 中未显示进度条

】AsyncTaskonPreExecute()中未显示进度条【英文标题】:ProgressBarnotshowinginAsyncTaskonPreExecute()【发布时间】:2014-10-2305:49:59【问题描述】:我目前的项目涉及从服务器下载数据-这大约需要20秒,所以在此期间我想显示一个进度条,以便... 查看详情

不可见的进度条未通过代码显示

】不可见的进度条未通过代码显示【英文标题】:InvisibleProgressBarnotshowingupthroughcode【发布时间】:2018-12-1316:07:26【问题描述】:我正在制作一个项目Android应用程序,它采用图像URL、下载图像并显示图像。如果图像尺寸较大,我... 查看详情

NSURLSession 下载任务 - 进度条问题

...处理一个非ARC项目,我必须在其中下载视频,下载后我将显示它们。我正在使用NSURLSession下载它。问题在进度条中。当我开始下载第四个文件时,我可以通过进度条正确下载前两个或三个文件,进度条没有正确更新,但文件与最... 查看详情

水平进度条不适用于 Asynctask Android 下载文件?

...】:2014-07-2417:50:27【问题描述】:我正在尝试使用Asynctask显示水平进度条,但它不适合我。它不是显示百分比进度量。我把我的xml和活动文件以及屏幕截图文件放在这里。布局xml<Relative 查看详情

关于vb.net中进度条使用问题

...条控件,现想在按下下载按钮下载资源的同时,用进度条显示下载进度,该如何写代码,谢谢。VB.Net中提供了ProgressBar控件,用于显示进度条设置ProgressBar的Minimum和Maximum属性为循环的起始和终止数值然后在代码的循环体中计算当... 查看详情

ios进度条显示

一、实现下载文件进度控制1.代码示例1#import"YYViewController.h"23@interfaceYYViewController()4@property(nonatomic,strong)NSMutableData*fileData;5@property(nonatomic,strong)NSFileHandle*writeHandle;6@property(nonatomic 查看详情

使用newxmlhttprequest()制作下载文件进度条

...器包含.mui-progressbar类,则以当前容器为目标控件,直接显示进度;否则,检查当前容器的直接孩子节点是否包含.mui-progressbar类,若存在,则以遍历到的第一个含有.mui-progressbar类的孩子节点为目标控件,显示进度;若当前容器的... 查看详情

长度在进度条中显示减号

】长度在进度条中显示减号【英文标题】:lengthshowsminusinprogressbar【发布时间】:2019-11-1814:20:10【问题描述】:我正在从URL下载视频,当我试图获取进度时它显示负值,在下面我添加了我的代码,byte[]buffer=newbyte[1024];intlen1=0;longto... 查看详情