2017-08-17 75 views
1

我想下載android webview應用程序中的特定文件(儘管它們來自外部鏈接而不是來自我的webview應用程序URL)以及所有外部鏈接應該打開外部webview應用程序的手機瀏覽器。Webview應用程序:如何在webview應用程序和外部鏈接下載文件應該在應用程序外部打開

我用下面的代碼來做到這一點。

wv.setWebViewClient(new WebViewClient(){ 


     public boolean shouldOverrideUrlLoading(WebView view, String url) { 

      boolean value = true; 
      String extension = MimeTypeMap.getFileExtensionFromUrl(url); 
      if (extension != null) { 
       MimeTypeMap mime = MimeTypeMap.getSingleton(); 
       String mimeType = mime.getMimeTypeFromExtension(extension); 
       if (mimeType != null) { 
        if (mimeType.toLowerCase().contains("pdf") 
          || extension.toLowerCase().contains("ppt") 
          || extension.toLowerCase().contains("doc") 
          || extension.toLowerCase().contains("rar") 
          || extension.toLowerCase().contains("rtf") 
          || extension.toLowerCase().contains("exe") 
          || extension.toLowerCase().contains("apk") 
          || extension.toLowerCase().contains("jpeg") 
          || extension.toLowerCase().contains("png") 
          || extension.toLowerCase().contains("xls") 
          || extension.toLowerCase().contains("zip") 
          || extension.toLowerCase().contains("jpg")) { 
         DownloadManager mdDownloadManager = (DownloadManager)getSystemService(Context.DOWNLOAD_SERVICE); 
         DownloadManager.Request request = new DownloadManager.Request(
           Uri.parse(url)); 
         String name= URLUtil.guessFileName(url,null,MimeTypeMap.getFileExtensionFromUrl(url)); 
         File destinationFile = new File(Environment.getExternalStorageDirectory(),name); 
         request.setDescription("Downloading..."); 
         request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); 
         // request.setDestinationUri(Uri.fromFile(destinationFile)); 
         request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,name); 
         mdDownloadManager.enqueue(request); 
         //value = false; 
        } 
       } 
       if (value) { 
        view.loadUrl(url); 
       } 

       if (!url.contains("my site url")) { // Could be cleverer and use a regex 
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); 
        startActivity(intent); 
        return true; 
       } 
       return false; 
      } 
      return false; 

     } 


     @Override 
     public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { 
      wv.loadUrl(mypage_error); 
     } 
    } 

當我點擊上面包含任何擴展鏈接,web視圖的應用程序(這是確定)內,但在同一時間鏈接自動打開了我的手機瀏覽器的文件開始下載。我知道這是因爲這個代碼。

if (!url.contains("my site url")) { // Could be cleverer and use a regex 
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); 
        startActivity(intent); 
        return true; 
       } 
       return false; 

現在,

首先解決我想知道我怎麼可以下載包含上述擴展的WebView應用程序和所有鏈接中的文件不包含上述延期應我的WebView應用程序外打開,這意味着移動瀏覽器。

其次解決方案 如果是不可能的,那麼告訴我,當我點擊鏈接如何顯示警報/彈出(瀏覽器選擇選項)每次。正如我所提到的,而不是顯示瀏覽器選擇警報鏈接自動打開到我的手機瀏覽器。

回答

1
public boolean shouldOverrideUrlLoading(WebView view, String url) { 

     boolean value = true; 
     String extension = MimeTypeMap.getFileExtensionFromUrl(url); 
     if (extension != null) { 
      MimeTypeMap mime = MimeTypeMap.getSingleton(); 
      String mimeType = mime.getMimeTypeFromExtension(extension); 
      if (mimeType != null) { 
       if (mimeType.toLowerCase().contains("pdf") 
         || extension.toLowerCase().contains("ppt") 
         || extension.toLowerCase().contains("doc") 
         || extension.toLowerCase().contains("rar") 
         || extension.toLowerCase().contains("rtf") 
         || extension.toLowerCase().contains("exe") 
         || extension.toLowerCase().contains("apk") 
         || extension.toLowerCase().contains("jpeg") 
         || extension.toLowerCase().contains("png") 
         || extension.toLowerCase().contains("xls") 
         || extension.toLowerCase().contains("zip") 
         || extension.toLowerCase().contains("jpg")) { 
        DownloadManager mdDownloadManager = (DownloadManager)getSystemService(Context.DOWNLOAD_SERVICE); 
        DownloadManager.Request request = new DownloadManager.Request(
          Uri.parse(url)); 
        String name= URLUtil.guessFileName(url,null,MimeTypeMap.getFileExtensionFromUrl(url)); 
        File destinationFile = new File(Environment.getExternalStorageDirectory(),name); 
        request.setDescription("Downloading..."); 
        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); 
        // request.setDestinationUri(Uri.fromFile(destinationFile)); 
        request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,name); 
        mdDownloadManager.enqueue(request); 
        //value = false; 
       } 
      } 
      if (value) { 
       view.loadUrl(url); 
      } 

      if (!url.contains("my site url")) { // Could be cleverer and use a regex 
       Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); 
       startActivity(intent); 
       return true; 
      }else{ 
      new AlertDialog.Builder(this) 
      .setIcon(android.R.drawable.ic_dialog_alert) 
      .setTitle("Exit!") 
      .setMessage("Are you sure you want to close?") 
      .setPositiveButton("Yes", new DialogInterface.OnClickListener() 
      { 
       @Override 
       public void onClick(DialogInterface dialog, int which) { 

        finish(); 

       } 

      }) 
      .setNegativeButton("No", null) 
      .show(); 
       } 
      return false; 
     } 
     return false; 

    } 

替換正面和負面的按鈕代碼來做任何你喜歡的事情。 我希望這能解答你的用例。

+0

在**(this)顯示錯誤**'新的AlertDialog.Builder(this)' – user3137451

+0

您可以發佈整個堆棧跟蹤日誌報告 –

+0

好吧,我從這裏獲得了第二個解決方案**(https:// stackoverflow.com/questions/28492579/give-the-option-to-always-choose-a-browser-to-open-a-link)但**第一個解決方案**會更好 – user3137451

相關問題