2

This is more a question out of curiosity than a real problem that needs to be solved. I made an Android app which contains a WebView. I used the should override URL method so that any link clicked would be opened in the WebView.

Later I decided that a file would be downloaded from the server to the user device. Unfortunately I had not seen the setDownloadListener method before. When the user clicks a link now the download is not initiated.

As far as I can tell I need to update the app with proper code i.e the download listener or a HttpClient, which is okay.

(At the risk of sounding like an idiot) I am wondering, is there any way through an action from the server that I can make the WebView download a file without a code change?

I guess that functionality is not in the WebView which is probably why the WebView opens a browser to download a file. Just thinking maybe I have missed something to make it work. I am pretty new to this.

2 Answers 2

3

You should be able to fire a download with DownloadManager in android via a webview JavascripInterface. I am trying this myself with no success so far. I probably have issues with the context in which the webview and DownloadManager are working. I will come back with some code if it's going to work :)

UPDATE:

The following code works very well for me. You can use something like: (just beaware you can only use DownloadManager from Android 2.3+)

myWebView.setWebViewClient(new WebViewClient() {
        @Override
        public void onReceivedError(WebView view, int errorCode,
            String description, String failingUrl) {
                Log.d("WEB_VIEW_TEST", "error code:" + errorCode + " - " + description);
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
                // handle different requests for different type of files
                // this example handles downloads requests for .apk and .mp3 files
                // everything else the webview can handle normally
                if (url.endsWith(".apk")) {
                    Uri source = Uri.parse(url);
                    // Make a new request pointing to the .apk url
                    DownloadManager.Request request = new DownloadManager.Request(source);
                    // appears the same in Notification bar while downloading
                    request.setDescription("Description for the DownloadManager Bar");
                    request.setTitle("YourApp.apk");
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
                        request.allowScanningByMediaScanner();
                        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
                    }
                    // save the file in the "Downloads" folder of SDCARD
                    request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "SmartPigs.apk");
                    // get download service and enqueue file
                    DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
                    manager.enqueue(request);
                }
                else if(url.endsWith(".mp3")) {
                    // if the link points to an .mp3 resource do something else
                }
                // if there is a link to anything else than .apk or .mp3 load the URL in the webview
                else view.loadUrl(url);
                return true;                
        }
    });

In the above code I manage links to .apk files, .mp3 files and all the other links will be handled by the webview (normal HTML pages)

1

I think webview will download and render only text/html contents and the multiparts. And the rest will be directed to a download client which you are seeing.

  • Herojit
1
  • You're right. It doesn't actually direct to a download client. I have to call that download intent myself but you are right that it only downloads the html. Dec 17, 2011 at 23:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.