2015-09-18 63 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

有免費的午餐沒有這樣的事情