4

I have an attachment on WebView. When I tap on it, nothing happens. I do know way to open attachment on WebView but the solution is condition based. Is there some solution to open it without placing condition because there are number of extensions attachment is supported in my app. I don't want the attachment to be downloaded.
This is what I am doing right now and these are just few of the extensions:

if ((url.contains(".pdf") || url.contains(".xml") || url.contains(".xlsx") || url.contains(".docx") || url.contains(".ppt"))) {
                            url = org.apache.commons.lang3.StringUtils.join("http://docs.google.com/gview?embedded=true&url=", url);
                            browser.loadUrl(url);
                        }
Community
  • 1
  • 1
Nitish
  • 13,845
  • 28
  • 135
  • 263

1 Answers1

2

What you want is only partial possible, and would always need exception handling. In the Android Webview, you can do the following things with regards to handling a link click:

1: Setting a webviewclient to intercept any clicked url:

Setting the web client allows you to check which url is clicked, and specify an action for each different url.

webview.setWebViewClient(new WebViewClient() {
    public boolean shouldOverrideUrlLoading(WebView view, String url){
        // you can try to open the url, you can also handle special urls here
        view.loadUrl(url);
        return false; // do not handle by default action
   }
});

You can make this as difficult as you like, to handle very specific file types that really need to be downloaded first, but to download them in the background without loading the external browser you can do something like this:

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
       // handle different requests for different type of files
       // Download url when it is a music file
       if (url.endsWith(".mp3")) {
           Uri source = Uri.parse(url);
           DownloadManager.Request mp3req = new DownloadManager.Request(source);
           // appears the same in Notification bar while downloading
           mp3req.setDescription("Downloading mp3..");
           mp3req.setTitle("song.mp3");
           if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
               mp3req.allowScanningByMediaScanner();
               mp3req.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
           }                   
           mp3req.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "song.mp3");
           DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
           manager.enqueue(mp3req);
      }
      else if(url.endsWith(".something")) {
          // do something else
      }
      //or just load the url in the web view
      else view.loadUrl(url);
      return true;                
}

2: Intercept any downloaded file:

You can also intercept when a download is being started by using this code. That way you can directly use the downloaded content in your app.

mWebView.setDownloadListener(new DownloadListener() {
    public void onDownloadStart(String url, String userAgent,
                String contentDisposition, String mimetype,
                long contentLength) {
        //do whatever you like with the file just being downloaded

    }
});

No Guarantees, exception handling is always needed

Content types that can be handled by the WebView, are depending on the version of WebView being used, at the current point in time, the WebView can handle only certain types. For some types special permissions are needed or hardware acceleration for html5 video for example. Another example of support: SVG was not supported before Android 3.0. And there are many many other examples where support for certain types have been implemented in the recent versions of WebView but do not exist for older versions.

You can read more about the current WebView implementation here: https://developer.chrome.com/multidevice/webview/overview

There is no such thing as a free lunch

Niki van Stein
  • 10,564
  • 3
  • 29
  • 62