WebView文件下载

本文介绍了如何在Android应用中使用WebView组件实现文件下载功能。通过设置监听器并重写onDownloadStart方法,可以选择直接调用系统浏览器下载或自定义下载流程。自定义流程包括创建线程下载文件,并指定文件保存路径。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 本节给大家介绍的是WebView下载文件的知识点,当我们在使用普通浏览器的时候,比如UC, 当我们点击到一个可供下载链接的时候,就会进行下载,WebView作为一个浏览器般的组件, 当然也是支持下载,我们可以自己来写下载的流程,设置下载后的文件放哪,以什么文件名 保存,当然也可以调用其它内置的浏览器来进行下载

 

这个很简单,我们只需为WebView设置setDownloadListener,然后重写DownloadListener的 onDownloadStart,然后在里面写个Intent,然后startActivity对应的Activity即可。

 

 

1.设置监听

webView.setDownloadListener(new MyDownloadListener());

 

 

2.监听中捕获下载Action抛给浏览器执行

/**
 * DownloadListener实现类 实现WebView下载功能
* */

private class MyDownloadListener implements DownloadListener{
    @Override
    public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
        Uri uri = Uri.parse(url);
        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
        startActivity(intent);
   }
}

 

3.或者自己写线程下载

package com.wjn.customwebviewjs;

import android.os.Environment;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class DownLoadThread implements Runnable {


    private String dlUrl;
    public DownLoadThread(String dlUrl) {
        this.dlUrl = dlUrl;
    }

    @Override
    public void run() {
        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)) {
                downloadFile = Environment.getExternalStorageDirectory();
                sdFile = new File(downloadFile, "csdn_client.apk");
                fout = new FileOutputStream(sdFile);
            } else {
                toast.show("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();
                }
            }
        }
    }
}

 

4.然后监听中操作

/**
 * DownloadListener实现类 实现WebView下载功能
* */

private class MyDownloadListener implements DownloadListener{
    @Override
    public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
        new Thread(new DownLoadThread(url)).start();
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值