Question

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
Was it helpful?

Solution

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top