
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,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,count);
7-15%下载进度条对话框关闭后,我看到“完成!”在日志中.为什么?
解决方法 您应该看看实现http范围.这将允许您在失败时重新启动下载.至于它为什么停止,一些 *** 作符实施下载限制,将在一定时间或下载量之后删除连接.我曾在一家英国航空公司看到这个第一手牌,并被别人告诉过.通过尝试通过AndroIDs浏览器下载文件通常很容易验证限制,如果您看到它停滞或停止,您知道它最有可能是 *** 作符问题(是的,下载可以终止,而不会抛出异常).
一个http范围的实现将允许您继续下载从它所在的地方,因此您的doInBackground方法使用暂停和恢复算法,以便每次连接中断您等待一段时间,然后尝试恢复在哪里下载不再(当然,实施重试限制,所以当手机真的无法下载文件时,您不会遇到无限循环).
总结以上是内存溢出为你收集整理的Android:下载大文件全部内容,希望文章能够帮你解决Android:下载大文件所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)