三种方式让 Android WebView 支持文件下载
三種方式讓 Android WebView 支持文件下載
?
最近在開發的過程中遇到一個需求,那就是讓 WebView 支持文件下載,比如說下載 apk。WebView 默認是不支持下載的,需要開發者自己實現。既然 PM 提出了需求,那咱就擼起袖子干唄,于是乎在網上尋找了幾種方法,主要思路有這么幾種:
- 跳轉瀏覽器下載
- 使用系統的下載服務
- 自定義下載任務
有了思路就好辦了,下面介紹具體實現。
要想讓 WebView 支持下載,需要給 WebView 設置下載監聽器?setDownloadListener,DownloadListener 里面只有一個方法 onDownloadStart,每當有文件需要下載時,該方法就會被回調,下載的 URL 通過方法參數傳遞,我們可以在這里處理下載事件。
?
mWebView.setDownloadListener(new DownloadListener() {@Overridepublic void onDownloadStart(String url, String userAgent, String contentDisposition, String mimeType, long contentLength) {// TODO: 2017-5-6 處理下載事件}});1. 跳轉瀏覽器下載
這種方式最為簡單粗暴,直接把下載任務拋給瀏覽器,剩下的就不用我們管了。缺點是無法感知下載完成,當然就沒有后續的處理,比如下載 apk 完成后打開安裝界面。
private void downloadByBrowser(String url) {Intent intent = new Intent(Intent.ACTION_VIEW);intent.addCategory(Intent.CATEGORY_BROWSABLE);intent.setData(Uri.parse(url));startActivity(intent);}?
?
2. 使用系統的下載服務
DownloadManager 是系統提供的用于處理下載的服務,使用者只需提供下載 URI 和存儲路徑,并進行簡單的設置。DownloadManager 會在后臺進行下載,并且在下載失敗、網絡切換以及系統重啟后嘗試重新下載。
? private void downloadBySystem(String url, String contentDisposition, String mimeType) {// 指定下載地址DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));// 允許媒體掃描,根據下載的文件類型被加入相冊、音樂等媒體庫request.allowScanningByMediaScanner();// 設置通知的顯示類型,下載進行時和完成后顯示通知request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);// 設置通知欄的標題,如果不設置,默認使用文件名// request.setTitle("This is title");// 設置通知欄的描述// request.setDescription("This is description");// 允許在計費流量下下載request.setAllowedOverMetered(false);// 允許該記錄在下載管理界面可見request.setVisibleInDownloadsUi(false);// 允許漫游時下載request.setAllowedOverRoaming(true);// 允許下載的網路類型request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI);// 設置下載文件保存的路徑和文件名String fileName = URLUtil.guessFileName(url, contentDisposition, mimeType);log.debug("fileName:{}", fileName);request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName);// 另外可選一下方法,自定義下載路徑// request.setDestinationUri()// request.setDestinationInExternalFilesDir()final DownloadManager downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);// 添加一個下載任務long downloadId = downloadManager.enqueue(request);log.debug("downloadId:{}", downloadId);}這樣我們就添加了一項下載任務,然后就靜靜等待系統下載完成吧。還要注意一點,別忘了添加讀寫外置存儲權限和網絡權限哦~
那怎么知道文件下載成功呢?系統在下載完成后會發送一條廣播,里面有任務 ID,告訴調用者任務完成,通過 DownloadManager 獲取到文件信息就可以進一步處理。
Ok,到這里,利用系統服務下載就算結束了,簡單總結一下。我們只關心開始和完成,至于下載過程中的暫停、重試等機制,系統已經幫我們做好了,是不是非常友好?
3. 自定義下載任務
有了下載鏈接就可以自己實現網絡部分,我在這兒自定義了一個下載任務,使用 HttpURLConnection 和 AsyncTask 實現,代碼還是比較簡單的。
? private class DownloadTask extends AsyncTask<String, Void, Void> {// 傳遞兩個參數:URL 和 目標路徑private String url;private String destPath;@Overrideprotected void onPreExecute() {log.info("開始下載");}@Overrideprotected Void doInBackground(String... params) {log.debug("doInBackground. url:{}, dest:{}", params[0], params[1]);url = params[0];destPath = params[1];OutputStream out = null;HttpURLConnection urlConnection = null;try {URL url = new URL(params[0]);urlConnection = (HttpURLConnection) url.openConnection();urlConnection.setConnectTimeout(15000);urlConnection.setReadTimeout(15000);InputStream in = urlConnection.getInputStream();out = new FileOutputStream(params[1]);byte[] buffer = new byte[10 * 1024];int len;while ((len = in.read(buffer)) != -1) {out.write(buffer, 0, len);}in.close();} catch (IOException e) {log.warn(e);} finally {if (urlConnection != null) {urlConnection.disconnect();}if (out != null) {try {out.close();} catch (IOException e) {log.warn(e);}}}return null;}@Overrideprotected void onPostExecute(Void aVoid) {log.info("完成下載");Intent handlerIntent = new Intent(Intent.ACTION_VIEW);String mimeType = getMIMEType(url);Uri uri = Uri.fromFile(new File(destPath));log.debug("mimiType:{}, uri:{}", mimeType, uri);handlerIntent.setDataAndType(uri, mimeType);startActivity(handlerIntent);}}private String getMIMEType(String url) {String type = null;String extension = MimeTypeMap.getFileExtensionFromUrl(url);log.debug("extension:{}", extension);if (extension != null) {type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);}return type;}// 使用mWebView.setDownloadListener(new DownloadListener() {@Overridepublic void onDownloadStart(String url, String userAgent, String contentDisposition, String mimeType, long contentLength) {String fileName = URLUtil.guessFileName(url, contentDisposition, mimeType);String destPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath() + File.separator + fileName;new DownloadTask().execute(url, destPath);}});優勢是我們可以感知下載進度,處理開始、取消、失敗、完成等事件,不足之處是對下載的控制不如系統服務,必須自己處理網絡帶來的問題。
可以看出,這三種下載方式各有特點,大家可以根據需要選擇。
總結
以上是生活随笔為你收集整理的三种方式让 Android WebView 支持文件下载的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2022年G2电站锅炉司炉复习题及模拟考
- 下一篇: android sina oauth2.