Android:下载大文件 [英] Android: download large file

查看:56
本文介绍了Android:下载大文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 Internet 下载大文件 (>20Mb)

I'm trying to download large file from Internet (>20Mb)

private class DownloadTask extends AsyncTask<DatabaseInfo, Integer, String> {

    private DatabaseInfo info;

    protected String doInBackground(DatabaseInfo... dbInfo) {

        int count;
        info = dbInfo[0];

        try {

            URL url = new URL(dbInfo[0].dbPath);

            InputStream input = new BufferedInputStream(url.openStream());
            OutputStream output = new FileOutputStream("/sdcard/db.zip");

            byte data[] = new byte[1024];
            int total = 0;

            while ((count = input.read(data)) != -1) {

                //output.write(data, 0, count);

                total += count;

                if (total % 10240 == 0) {
                    publishProgress(total);
                }
            }

            output.flush();
            output.close();
            input.close();
        } 
        catch (Exception e) {
            Log.e("err", e.getMessage());
        }

        return null;
    }

    protected void onProgressUpdate(Integer... total) {

        int perc = (int) ((float) total[0] / (float) info.dbZipSize * 100);
        mProgressDialog.setProgress(perc);
    }

    protected void onPostExecute(String s) {

        dismissDialog(DIALOG_PROGRESS);
        Log.e("err", "finish!");
    }
}

如果我取消注释行

//output.write(data, 0, count);

在 7-15% 下载进度条对话框关闭后,我看到完成!"在日志中.为什么?

after 7-15% downloading progressbar dialog dismiss and I see "finish!" in Log. Why?

推荐答案

您应该考虑实现 HTTP 范围.这将允许您在下载失败时重新开始下载.

You should look at implementing HTTP Ranges. This will allow you to re-start your download when it fails.

至于为什么会停止,一些运营商实施了下载限制,这会在一定时间或下载量后断开连接.我在一家英国运营商上亲眼目睹了这一点,并在其他运营商身上被告知.通过尝试通过 Android 浏览器下载文件通常很容易验证限制,如果您看到它停止或停止,您就知道这很可能是运营商问题(是的,可以在不引发异常的情况下终止下载).

As to why it stops, some carriers implement download limits which will drop a connection after a certain time or download amount. I've seen this first hand on one UK carrier and been told about it on others. It's usually easy to verify the limit by trying to download the file via Androids browser and if you see it stall or stop you know it's most likely a carrier issue (and yes, the download can be terminated without throwing an Exception).

Http Range 实现将允许您从停止的地方继续下载,因此您的 doInBackground 方法使用延迟和恢复算法,以便每次连接中断时您等待一段时间然后尝试从下载停止的地方继续(当然,还要实施重试限制,这样当手机真的无法下载文件时,您就不会陷入无限循环).

An Http Range implementation will allow you to continue the download from where it left off and thus your doInBackground method to use a hold-off and resume algorithm so that every time the connection is interrupted you wait for an amount of time then try to resume where the download left off (and, of course, implement a retry limit so you don't end up with an infinite loop when the phone really can't download the file).

这篇关于Android:下载大文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆