[英]Android Download Manager - can't know size of download
我在嘗試從基於WebView的應用程序中的服務器上下載文件時遇到問題。 此代碼實際上適用於具有Android 4.4.2 的Galaxy S4 , 但不適用於具有相同版本Android 的Nexus 5 。
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.toLowerCase().contains("video") || url.toLowerCase().contains("mp4")) {
String cookie = CookieManager.getInstance().getCookie(url);
DownloadManager mdDownloadManager = (DownloadManager) MainActivity.this
.getSystemService(Context.DOWNLOAD_SERVICE);
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.addRequestHeader("Cookie", cookie);
request.setDescription(getString(R.string.download_video));
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setTitle(getString(R.string.app_name));
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).mkdirs();
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, getFileName(url));
request.setMimeType("video/mp4");
mdDownloadManager.enqueue(request);
}
webViewPreviousState = PAGE_REDIRECTED;
view.loadUrl(appendAndroidClientIdentification(url));
return true;
}
在嘗試通過Nexus 5設備下載文件時,檢查日志發現了這一點:
I/DownloadManager﹕ Download 1755 starting
W/DownloadManager﹕ Aborting request for download 1755: can't know size of download, giving up
I/DownloadManager﹕ Download 1755 finished with status CANNOT_RESUME
問題出在服務器端。 我正在使用Rails的send_data方法。 此方法未在標頭中發送ContentLength,這是某些設備中錯誤的原因。
要解決此問題,只需將其添加到application.rb文件中:
config.middleware.use Rack::ContentLength
問題出在服務器端。 我正在使用send_data
方法。 此方法未在標頭中發送ContentLength
,這是某些設備中錯誤的原因。
要解決此問題,只需將其添加到application.rb
:
config.middleware.use Rack::ContentLength
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.