1

I´m having issues trying to download files from the server on a WebView based app. This code actually works on a Galaxy S4 with android 4.4.2 but doesn't work on a Nexus 5 with the same version of Android.

    @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;
    }

Inspecting the logs found this while trying to download a file via the Nexus 5 device:

 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

Answer:

The issue was on the server side. I was using the method send_data from rails. This method doesn't send the ContentLength in the headers and this is the reason of the error in some devices.

To fix this just add to the application.rb file :

config.middleware.use Rack::ContentLength
rodeleon
  • 362
  • 3
  • 18
  • 1
    Beyond the cookie, is there anything unusual about the HTTP request and response? Looking at the source code, the download code did not get a `Content-Length` header and did not get a `Transfer-Encoding: chunked` header, and so it does not know how big the download is. – CommonsWare Apr 13 '14 at 23:26
  • I'm testing this in my local network and the video files are serving by a rails app using the method send_data. Now, i try to download the file using chrome browser(it is a jquery mobile app) and it doesn't work and get the same error in the logs. So the Android code from the question may not be the problem... About your question the url which the rails app is getting the video file is an https one. If this is the problem i'm not sure why it works fine on S4 but not on Nexus 5... – rodeleon Apr 14 '14 at 16:42
  • Just update with the answer. Thanks for your help! – rodeleon Apr 14 '14 at 17:12

1 Answers1

2

The issue was on the server side. I was using the method send_data from rails. This method doesn't send the ContentLength in the headers and this is the reason of the error in some devices.

To fix this just add to the application.rb :

config.middleware.use Rack::ContentLength
rodeleon
  • 362
  • 3
  • 18