Android WebView file download

Introduction to this section

This section introduces to you the knowledge points of WebView downloading files. When we use a common browser, such as UC, when we click on a downloadable link, it will be downloaded. As a browser, WebView Of course, it also supports downloading. We can write the downloading process by ourselves, set where to put the downloaded file, and save it with what file name. Of course, we 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, then rewrite the onDownloadStart of DownloadListener, 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 mobile phone, a dialog box will open for you to choose one of the browsers to download~


2. Write your own thread to download files

Of course, you may not want to put the downloaded file in the default path, or you want to define the file name yourself, etc., you can write a thread to download the file yourself, and the implementation sample code is as follows:

Core code :

Let's write another downloaded 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 card can be written"); 
                downloadFile = Environment.getExternalStorageDirectory(); 
                sdFile = new File( downloadFile, "csdn_client.apk"); 
                fout = new FileOutputStream(sdFile); 
            }else{ 
                Log.e("HEHE","SD card does not exist or cannot be read and written"); 
            } 
            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:

Note :

Ok, also, don't forget to write read and write permissions for the SD card and permissions for Internet access to the network:

<uses-permission android:name="android.permission.INTERNET"/> 
<!-- Create and delete file permissions in SDCard --> 
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/> 
< !-- Write data permission to SDCard --> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

Also, in = conn.getInputStream() ; should be written after conn has set everything up! Remember, otherwise nothing will be read!

Guess you like

Origin blog.csdn.net/leyang0910/article/details/131554032