【问题标题】:HttpEntity.getContent() Progress Indicator?HttpEntity.getContent() 进度指示器?
【发布时间】:2011-07-19 17:17:57
【问题描述】:

在 android API 7+ 中有什么方法可以报告对 HttpEntity.getContent() 的调用进度吗?

在我的情况下,我在响应流中获得了一张图像,因此传输可能需要很长时间。我想要一个样式设置为STYLE_HORIZONTAL 的ProgressDialog 随着传输进度的更新。

有什么办法吗?

谢谢!

【问题讨论】:

    标签: android multithreading streaming progressdialog


    【解决方案1】:

    您是否尝试过HttpEntity.getContentLength() 来帮助您预先确定文件的大小?如果你将它与 AsyncTask 的 onProgressUpdate() 之类的东西结合使用,你应该能够实现它。

    看看这个链接

    Download a file with Android, and showing the progress in a ProgressDialog

    它几乎有你要找的东西。它使用 urlConnection 来获取 InputStream,因此您需要对其进行调整以使用 HttpEntity。所以也许你会有这样的东西。

    @Override
    protected String doInBackground(String... aurl) {
        int count;
        long contentLength = <yourHttpEntity>.getContentLength();
        InputStream input = new BufferedInputStream(<yourHttpEntity>.getContent());
        OutputStream output = new FileOutputStream("/sdcard/your_photo.jpg");
    
        byte data[] = new byte[1024];
    
        long total = 0;
        while ((count = input.read(data)) != -1) {
            total += count;
            publishProgress(""+(int)((total*100)/contentLength));
            output.write(data, 0, count);
        }
    
        output.flush();
        output.close();
        input.close();
    }
    

    并在 onProgressUpdate 中更新您的对话框。

    【讨论】:

    • 你确实应该感谢this答案的作者。
    猜你喜欢
    • 1970-01-01
    • 2011-09-29
    • 2021-05-10
    • 2017-12-12
    • 1970-01-01
    • 1970-01-01
    • 2020-03-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多