Downloading a file to Android WebView (without the download event or HTTPClient in the code)

后端 未结 2 1179
遇见更好的自我
遇见更好的自我 2021-01-15 05:28

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 s

相关标签:
2条回答
  • 2021-01-15 06:12

    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)

    0 讨论(0)
  • 2021-01-15 06:18

    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
    0 讨论(0)
提交回复
热议问题