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

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

问题描述

我想下载从互联网大文件(> 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.

至于为什么它停止,一些运营商实施下载限制,这将下降经过一定时间或下载量的连接。我亲眼目睹了一次手,一家英国运营商和被告知这件事别人。它通常容易被试图下载通过机器人浏览器的文件,以验证了极限,如果你看到它停止或阻止你知道这是最有可能是运营商的问题(是的,下载可能未抛出异常终止)。

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范围实施,将让你继续从那里离开,下载这样的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).

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

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