Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
441 views
in Technique[技术] by (71.8m points)

asynchronous - Android: download large file

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!");
    }
}

If I uncomment line

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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).

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).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...