dvdciri February 2016

Click link on a WebView not working

i think the question is very easy, i've implemented a base WebView in my Android app and i need the user to click on a link and download a pdf from the web (doesn't matter if will be redirected to the browser app).

My code is not working, when i click on a link is not doing anything:

@Override
public void loadContent() {
    super.loadContent();

    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.getSettings().setLoadWithOverviewMode(true);
    mWebView.getSettings().setUseWideViewPort(true);
    mWebView.getSettings().setBuiltInZoomControls(true);

    mWebView.setWebViewClient(new WebViewClient() {

        public void onPageFinished(WebView view, String url) {
            onLoadCompleted(false);
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    });

    Logger.i("Loading newsletter with url: " + mNewsletterUrl);
    mWebView.loadUrl(mNewsletterUrl);
}

Answers


Hemant Tripathi February 2016

Use DownloadManager to download the file from server.

Also set below permissions in the manifest file :

<uses-permission android:name="android.permission.ACCESS_DOWNLOAD_MANAGER"/>

and

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Try below code :

mWebView.setDownloadListener(new DownloadListener() {
   public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
        Request downloadRequest = new Request(Uri.parse(url));
        request.allowScanningByMediaScanner();
        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
        request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "download"); 
        DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
        dm.enqueue(request);
    }
});

Post Status

Asked in February 2016
Viewed 1,784 times
Voted 5
Answered 1 times

Search




Leave an answer