我正试图从我的Yahoo!下载一个大文件 网站服务器显然是设置(不是由我)断开下载,如果他们没有在100秒内完成.该文件足够小,通常可以成功传输.在数据速率较慢且下载断开的情况下,有没有办法在发生断开连接的文件偏移处恢复URLConnection?这是代码:
// Setup connection.
URL url = new URL(strUrl[0]);
URLConnection cx = url.openConnection();
cx.connect();
// Setup streams and buffers.
int lengthFile = cx.getContentLength();
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream(strUrl[1]);
byte data[] = new byte[1024];
// Download file.
for (total=0; (count=input.read(data, 0, 1024)) != -1; total+=count) {
publishProgress((int)(total*100/lengthFile));
output.write(data, 0, count);
Log.d("AsyncDownloadFile", "bytes: " + total);
}
// Close streams.
output.flush();
output.close();
input.close();
Run Code Online (Sandbox Code Playgroud) 我知道这个问题是重复的,我没有得到自满的答案.我正在使用Android系统DownloadManager从网络下载文件,它可以正常下载文件.
private DownloadManager downloadManager;
Button start;
Button pause;
Button resume;
Run Code Online (Sandbox Code Playgroud)
初始化上面的所有按钮
downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
Uri Download_Uri = Uri
.parse("uri");
DownloadManager.Request request = new DownloadManager.Request(
Download_Uri);
Run Code Online (Sandbox Code Playgroud)
然后它已被放入下载队列.当转弯即将开始下载
我的问题是,是否有任何简单的方法来暂停下载和恢复下载
downloadManager.pause();
downloadManager.resume();
Run Code Online (Sandbox Code Playgroud)
提前致谢.
在我的应用程序中,我从服务器下载电影.其中一些非常大(4gb或更多).我试图将我自己的下载管理器作为一项服务实现,但它并没有放弃.在某些设备上,应用程序只是在没有任何通知的情况下崩溃,整体下载似乎太慢了.
所以,我想使用Android的默认DownloadManager,但我唯一的问题是我无法暂停/恢复它.
有没有办法实现呢?