我在我的@L_301_0@应用程序上下载.我使用本地网络连接,下载速度很慢.
这是我正在使用的代码:
URL url = new URL(ep.getfileURL());file destfile = new file(<path to sd card file>);URLConnection uCon = url.openConnection();inputStream is = uCon.getinputStream();OutputStream os = new fileOutputStream(destfile);int progress = 0;int lastProgress = 0;int totalSize = uCon.getContentLength();int downloadedSize = 0;byte[] buffer = new byte[4096];int count = -1;while((count = is.read(buffer)) != -1){ os.write(buffer, 0, count);downloadedSize = downloadedSize + count; progress = (int)(downloadedSize * 100.0 / totalSize); if(progress - lastProgress >= 5) { publishProgress(progress); lastProgress = progress; }}os.close();
你发现任何问题吗?谢谢.
编辑:
我使用你的建议测试了我的代码,我得到了这些结果:
# Download times tests #Without bufferedoutputDownloading file: 1 ms, Start downloadDownloading file: 179812 ms, Finished downloading 54687744 bytesDownloading file: end, 179813 msWith bufferedoutputDownloading file: 1 ms, Start downloadDownloading file: 178312 ms, Finished downloading 54687744 bytesDownloading file: end, 178313 msWith httpclIEntDownloading file: beginDownloading file: 1 ms, Start downloadDownloading file: 178241 ms, Finished downloading 54687744 bytesDownloading file: end, 178242 ms
因此,使用Buffered流或直接使用httpClIEnt,不会改变任何东西……
我也应该提到我的代码在AsyncTask中,所以publishProgress()实际上已经在一个单独的线程上运行了……
谢谢您的帮助.
解决方法:
您应该使用BufferedinputStream包装输入流.您可能会获得更多的几个字节的浅读取,缓冲流将减轻其中一些.我首先尝试缓冲输入和输出流,以减少 *** 作系统级写入延迟.
其次,我要小心你如何发布进度.您似乎限制了它出现的次数,但您可能希望将下载移动到自己的runnable中并使用执行程序服务进行下载,并可能启动另一个线程来评估所有下载的进度并触发进度必要的消息.
总结以上是内存溢出为你收集整理的Android慢速文件下载全部内容,希望文章能够帮你解决Android慢速文件下载所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)