2017-03-31 21 views
1

下面是我實現這將打開這對日期選擇 表格信息並具有下載PDF在底部的三個按鈕的鏈接網頁視圖代碼, xls和doc文件 下載在瀏覽器中運行良好,但在webview下載中沒有發生!文件下載是不是在Android的工作,我想這樣一來,

public class Reports_Visit_Statastics extends AppCompatActivity { 

WebView wb; 
String ReportsURL, title; 
@SuppressLint("SetJavaScriptEnabled") 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.visit_statastics_reports); 
    wb = (WebView) findViewById(webView); 
    wb.getSettings().setLoadsImagesAutomatically(true); 
    wb.getSettings().setJavaScriptEnabled(true); 
    wb.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY); 
    Bundle b = getIntent().getExtras(); 
    ReportsURL = b.getString("URL"); 
    title = b.getString("title"); 
    initToolbar(); 
    wb.setWebViewClient(new MyWebViewClient()); 
    wb.loadUrl(ReportsURL); 

} 

private class MyWebViewClient extends WebViewClient { 
    @Override 
    public void onReceivedError(WebView view, int errorCode, 
           String description, String failingUrl) { 
     Log.d("WEB_VIEW_TEST", "error code:" + errorCode + " - " + description); 
    } 

    @Override 
    public boolean shouldOverrideUrlLoading(WebView view, String url) { 
     // handle different requests for different type of files 
     // this example handles downloads requests for .apk and .mp3 files 
     // everything else the webview can handle normally 
     if (url.endsWith(".pdf")) { 
      Uri source = Uri.parse(url); 
      // Make a new request pointing to the .apk url 
      DownloadManager.Request request = new DownloadManager.Request(source); 
      // appears the same in Notification bar while downloading 
      request.setDescription("Description for the DownloadManager Bar"); 
      request.setTitle("Document"); 

       request.allowScanningByMediaScanner(); 
       request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); 

      // save the file in the "Downloads" folder of SDCARD 
      request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "Document.doc"); 
      // get download service and enqueue file 
      DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE); 
      manager.enqueue(request); 
     } else if (url.endsWith(".doc")) { 
      Uri source = Uri.parse(url); 
      // Make a new request pointing to the .apk url 
      DownloadManager.Request request = new DownloadManager.Request(source); 
      // appears the same in Notification bar while downloading 
      request.setDescription("Description for the DownloadManager Bar"); 
      request.setTitle("Document"); 

       request.allowScanningByMediaScanner(); 
       request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); 

      // save the file in the "Downloads" folder of SDCARD 
      request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "Document.doc"); 
      // get download service and enqueue file 
      DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE); 
      manager.enqueue(request); 
     } else if (url.endsWith(".xls")) { 
      Uri source = Uri.parse(url); 
      // Make a new request pointing to the .apk url 
      DownloadManager.Request request = new DownloadManager.Request(source); 
      // appears the same in Notification bar while downloading 
      request.setDescription("Description for the DownloadManager Bar"); 
      request.setTitle("Document"); 

       request.allowScanningByMediaScanner(); 
       request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); 

      // save the file in the "Downloads" folder of SDCARD 
      request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "Document.doc"); 
      // get download service and enqueue file 
      DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE); 
      manager.enqueue(request); 
     } 
     // if there is a link to anything else than .apk or .mp3 load the URL in the webview 
     else view.loadUrl(url); 
     return true; 
    } 
} 


private void initToolbar() { 

    final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 
    final ActionBar actionBar = getSupportActionBar(); 

    try { 
     if (actionBar != null) { 
      actionBar.setTitle(title); 
      actionBar.setHomeButtonEnabled(true); 
      actionBar.setDisplayHomeAsUpEnabled(true); 
     } 
    } catch (Exception e) { 
     Log.d("Doctor_master", e.toString()); 
    } 
} 



@Override 

public void onBackPressed() { 

    super.onBackPressed(); 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    int id = item.getItemId(); 

    if (id == android.R.id.home) { 
     onBackPressed(); 
     return true; 
    } 
    return super.onOptionsItemSelected(item); 
} 

} 
+0

沒有答案,但! –

+0

你有沒有試過這個解決方案。 http://stackoverflow.com/questions/33434532/android-webview-download-files-like-browsers-do – Noorul

+0

@Ahamed,感謝回覆我剛剛嘗試過,我也嘗試過內容的問題和接受答案的內容,但也沒有工作! –

回答

2

根據聊天的評論和討論。

我注意到shouldOverrideUrlLoading沒有被調用。由於,按照Android文檔shouldOverrideUrlLoading

給主機應用程序有機會接管控制,當一個新的 網址是關於當前WebView

但你的情況要加載因爲地址欄中的網址在WebView沒有變化,同時點擊三個鏈接中的任何一個。它只是調用javascript代碼javascript:__doPostBack('lnkPDF',''),它調用post方法來生成文件。如果您希望使用DownloadManager在通知區域中顯示通知時下載文件,則需要爲諸如http://或https://的文件創建動態靜態url。例如。 http://www.somedomain/may_be_session_id/some_random_file_number_valid_for_some_time_only/file_name.pdf

除了上面,讓網頁更改或重定向到新的URL,只有這樣,你將能夠捕捉到的URL中shouldOverrideUrlLoading

如果主機應用程序想要離開當前的WebView並處理url本身,請嘗試更改您在shouldOverrideUrlLoading中的返回實現,否則返回false。

相關問題