2015-09-18 64 views
4

我在WebView上有附件。当我点击它时,没有任何反应。我知道way在WebView上打开附件,但解决方案是基于条件的。是否有一些解决方案可以在不放置条件的情况下打开它,因为我的应用程序支持多个扩展附件。 我不希望下载附件。
这是我在做什么,现在和这些只是几个扩展:在WebView中打开附件

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); 
         } 
+1

总会有被附加条件,因为网页流量只能处理的内容数量有限类型。 – e4c5

+0

@ e4c5:放置条件是唯一的解决方案? – Nitish

+0

你在WebViewClient.shouldInterceptRequest中使用该代码吗? – Pollizzio

回答

2

你需要的是只是局部的可能,并总是需要异常处理。 在Android网页视图,你可以用在以下方面的事情处理链接点击:

1:设置webviewclient拦截任何点击网址:

设置Web客户端,您可以检查哪些网址被点击,并为每个不同的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 
    } 
}); 

可以使这个困难,因为你喜欢,来处理确实需要先下载非常特定的文件类型,但下载它们在后台,而无需加载外部浏览器,你可以这样做:

@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:拦截任何下载的文件:

当下载正在使用此代码开始您也可以拦截。这样,您可以直接在您的应用中使用下载的内容。

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 

    } 
}); 

没有保证,异常处理总是需要,可以通过web视图处理

内容类型,都依赖于的WebView的使用的版本,在当前时间点,WebView只能处理某些类型。例如,对于某些类型的特殊权限,或者对于html5视频需要hardware acceleration。 支持的另一个例子:在Android 3.0之前不支持SVG。另外还有许多其他示例,在最新版本的WebView中已经实现了对某些类型的支持,但旧版本中不存在这些示例。

你可以阅读更多关于当前的WebView执行此:https://developer.chrome.com/multidevice/webview/overview

有免费的午餐没有这样的事情