Android下载管理器 - 无法知道下载的大小

时间:2014-04-13 23:10:58

标签: java android download-manager android-download-manager

我在尝试从基于WebView的应用程序上的服务器下载文件时遇到问题。 这段代码实际上适用于Galaxy S4 ,其中包含android 4.4.2 ,但在使用相同版本的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

1 个答案:

答案 0 :(得分:2)

问题出在服务器端。我正在使用rails中的send_data方法。 此方法不会在标头中发送ContentLength,这是某些设备出错的原因。

要解决此问题,只需添加到application.rb

即可
config.middleware.use Rack::ContentLength
相关问题