首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >显示以百分比表示的进度对话框

显示以百分比表示的进度对话框
EN

Stack Overflow用户
提问于 2016-02-21 01:42:05
回答 2查看 2.3K关注 0票数 2

我正在使用异步任务从互联网下载一些文件。对于这个问题,我知道向用户展示到目前为止的进展是非常相关的。我下载文件的所有实现都非常成功,但唯一的问题是进度对话框,即使启动了下载命令,这也显示为0%。

我就是这样做的

代码语言:javascript
代码运行次数:0
运行
复制
 // Show Dialog Box with Progress bar
  @Override
  protected Dialog onCreateDialog(int id) {
    switch (id) {
        case progress_bar_type:
            prgDialog = new ProgressDialog(this);
            prgDialog.setMessage("Downloading Mp3 file. Please wait...");
            prgDialog.setIndeterminate(false);
            prgDialog.setMax(100);
            prgDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            prgDialog.setCancelable(false);
            prgDialog.show();
            return prgDialog;
        default:
            return null;
    }
}



// Async Task Class
class DownloadMusicfromInternet extends AsyncTask<String, String, String> {

    // Show Progress bar before downloading Music
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        // Shows Progress Bar Dialog and then call doInBackground method
        showDialog(progress_bar_type);
    }

    // Download Music File from Internet
    @Override
    protected String doInBackground(String... f_url) {
        int count;
        try {
            URL url = new URL(f_url[0]);
            URLConnection conection = url.openConnection();
            conection.connect();
            // Get Music file length
            int lenghtOfFile = conection.getContentLength();
            // input stream to read file - with 8k buffer
            InputStream input = new BufferedInputStream(url.openStream(),10*1024);
            // Output stream to write file in SD card
            OutputStream output = new FileOutputStream(f_url[1]);
            byte data[] = new byte[1024];
            long total = 0;
            while ((count = input.read(data)) != -1) {
                total += count;
                // Publish the progress which triggers onProgressUpdate method
                publishProgress("" + (int) ((total * 100) / lenghtOfFile));

                // Write data to file
                output.write(data, 0, count);
            }
            // Flush output
            output.flush();
            // Close streams
            output.close();
            input.close();
        } catch (Exception e) {
            Log.e("Error: ", e.getMessage());
        }
        return null;
    }

    // While Downloading Music File
    protected void onProgressUpdate(String... progress) {
        // Set progress percentage
        prgDialog.setProgress(Integer.parseInt(progress[0]));
    }

    // Once Music File is downloaded
    @Override
    protected void onPostExecute(String file_url) {
        // Dismiss the dialog after the Music file was downloaded
        dismissDialog(progress_bar_type);
        Toast.makeText(getApplicationContext(), "Download complete, playing Music", Toast.LENGTH_LONG).show();
    }
}

请根据下载时间的移动,如何使进度移动。谢谢你的帮助。

更新

我认为这个问题与所谓的重复问题不一样,我研究后的问题来自于int lenghtOfFile = conection.getContentLength();

进程不会移动,因为getContentLength()总是返回-1,因为它无法从服务器获得文件大小。我见过很多这样的问题,但都没有答案。请问有出路吗?我很想知道。先谢

EN

回答 2

Stack Overflow用户

发布于 2016-02-21 08:39:26

正如您在评论中所说的,getContentLength()正在返回-1。这将导致您的进度更新始终是负面的,这意味着进度条永远不会前进。这就是为什么它没有像你期望的那样移动。

根据javadoc,当没有内容长度头或由于某种原因不能将其解析为数字时,getContentLength()返回-1。在这种情况下,您无法提供基于下载文件大小的进度表。

票数 0
EN

Stack Overflow用户

发布于 2016-02-21 16:21:53

如果进度条设置为不确定,则不需要调用prgDialog.setProgress(Integer.parseInt(progress[0]));。这可能就是为什么没有显示出进步的原因。

下载开始时调用prgDialog.setIndeterminate(true),然后在下载结束时调用prgDialog.setIndeterminate(false)。在使用不确定进度条时,请避免调用setProgress

编辑:矩形:从官方文件调用,如果条形图处于不确定模式,setProgress什么也不做。

此外,您还说要将不确定值设置为true,但是在onCreateDialog方法中我看到了prgDialog.setIndeterminate(false);

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35531302

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档