WebView file download
Introduction to this section
This section introduces you to the knowledge points of WebView downloading files. When we use ordinary browsers, such as UC, When we click on a download link, it will be downloaded. As a browser-like component, WebView Of course, downloading is also supported. We can write the download process ourselves, set where to put the downloaded files, and what file name to use. Save, of course you can also call other built-in browsers to download, such as Chrome, UC, etc.! Let me show you how to use it!
1. Call other browsers to download files:
This is very simple, we only need to set setDownloadListener for WebView, and then rewrite the DownloadListener onDownloadStart, then write an Intent in it, and then start the Activity corresponding to Activity!
The key code is as follows:
wView.setDownloadListener(new DownloadListener(){ @Override public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) { Log.e("HEHE","开始下载"); Uri uri = Uri.parse(url); Intent intent = new Intent(Intent.ACTION_VIEW,uri); startActivity(intent); } });
If there are multiple browsers in your phone, a dialog box will open for you to choose one of the browsers. Download~
2. Write your own thread to download the file
Of course, you may not want to put the downloaded file in the default path, or you may want to define the file name yourself, etc. You can write it yourself A thread to download files, the implementation sample code is as follows:
Core code:
We write another downloading thread class ourselves:
DownLoadThread.java
/** * Created by Jay on 2015/9/14 0014. */ public class DownLoadThread implements Runnable { private String dlUrl; public DownLoadThread(String dlUrl) { this.dlUrl = dlUrl; } @Override public void run() { Log.e("HEHE", "开始下载~~~~~"); InputStream in = null; FileOutputStream fout = null; try { URL httpUrl = new URL(dlUrl); HttpURLConnection conn = (HttpURLConnection) httpUrl.openConnection(); conn.setDoInput(true); conn.setDoOutput(true); in = conn.getInputStream(); File downloadFile, sdFile; if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { Log.e("HEHE","SD卡可写"); downloadFile = Environment.getExternalStorageDirectory(); sdFile = new File(downloadFile, "csdn_client.apk"); fout = new FileOutputStream(sdFile); }else{ Log.e("HEHE","SD卡不存在或者不可读写"); } byte[] buffer = new byte[1024]; int len; while ((len = in.read(buffer)) != -1) { fout.write(buffer, 0, len); } } catch (Exception e) { e.printStackTrace(); } finally { if (in != null) { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } if (fout != null) { try { fout.close(); } catch (IOException e) { e.printStackTrace(); } } } Log.e("HEHE", "下载完毕~~~~"); } }
Then create and start the thread in MainActivity.java:
wView.setDownloadListener(new DownloadListener(){ @Override public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) { Log.e("HEHE","onDownloadStart被调用:下载链接:" + url); new Thread(new DownLoadThread(url)).start(); } });
Running result:
When we open the SD card, we can see that the downloaded file has been quietly lying in the SD card:
Notes:
Okay, in addition, don’t forget the read and write permissions for the SD card and the permissions for Internet access to the network:
Also, in = conn.getInputStream(); To be written after conn has set up everything! ! Remember, otherwise you won’t be able to read anything!
Summary of this section:
This section is very simple, the code will not be posted. In fact, it is the setDownloadListener thing, rewrite it yourself The onDownloadStart method is just used to handle the download process~, that’s it for this section, thank you~