Android中怎么利用WebView实现文件下载功能

发布时间:2021-06-27 15:30:32 作者:Leah
来源:亿速云 阅读:237

本篇文章给大家分享的是有关Android中怎么利用WebView实现文件下载功能,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

方法1,自定义下载操作

1. 先来布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ldm="http://schemas.android.com/apk/res/com.ldm.learn"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<WebView
android:id="@+id/test_wv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="15dp" />
</RelativeLayout>

2. 实现自定义下载工具操作异步线程类:

public class DownLoadThread extends Thread {
private String downLoadUrl;
private Context context;
private FileOutputStream out = null;
private File downLoadFile = null;
private File sdCardFile = null;
private InputStream in = null;
public DownLoadThread(String downLoadUrl, Context context) {
super();
this.downLoadUrl = downLoadUrl;
this.context = context;
}
@Override
public void run() {
try {
URL httpUrl = new URL(downLoadUrl);
HttpURLConnection conn = (HttpURLConnection) httpUrl.openConnection();
 conn.setDoInput(true);// 如果打算使用 URL 连接进行输入,则将 DoInput 标志设置为 true;如果不打算使用,则设置为 false。默认值为 true。
conn.setDoOutput(true);// 如果打算使用
 URL 连接进行输出,则将 DoOutput 标志设置为 true;如果不打算使用,则设置为 false。默认值为 false。
in = conn.getInputStream();
if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
 Toast.makeText(context, "SD卡不可用!", Toast.LENGTH_SHORT).show();
return;
}
downLoadFile = Environment.getExternalStorageDirectory();
sdCardFile = new File(downLoadFile, "download.apk");
out = new FileOutputStream(sdCardFile);
byte[] b = new byte[1024];
int len;
while ((len = in.read(b)) != -1) {
out.write(b, 0, len);
}
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
}
catch (Exception e) {
e.printStackTrace();
}
 }
}

3. 文件下载

public class MainActivity extends Activity {
private WebView test_wv;
private String downLoadUrl = "http://as.baidu.com/a/rank?cid=101&s=1&f=web_alad";
 @Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.test_wv
 = (WebView) findViewById(R.id.test_wv);
test_wv.loadUrl(downLoadUrl);
test_wv.setWebViewClient(new WebViewClient()
 {
@Override
public boolean shouldOverrideUrlLoading(WebView
 view, String url) {
view.loadUrl(url);
return super.shouldOverrideUrlLoading(view, url);
}
});
//要实现WebView文件下载,实现这个监听就ok
test_wv.setDownloadListener(new
 DownloadListener() {
@Override
public void onDownloadStart(String
 url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
Log.v("ldm", url);
if (url.endsWith(".apk")) {//判断是否是.apk结尾的文件路径
new DownLoadThread(url, MainActivity.this).start();
}
}
});
}
}

方法2:通过系统自身下载方式下载(会在通知栏显示下载进度条)

只需要把这个方法改写如下:

test_wv.setDownloadListener(new
 DownloadListener() {
@Override
public
 void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
Log.v("ldm",
 url);
Uri
 uri=Uri.parse(url);
Intent
 intent=new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
});

以上就是Android中怎么利用WebView实现文件下载功能,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。

推荐阅读:
  1. 怎么在Android中利用WebView实现点击拦截跳转功能
  2. 怎么在Android中利用Handler实现一个文件下载功能

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

android webview

上一篇:Android中怎么实现一个分享控件

下一篇:Android中怎么利用GET方法实现网络传值

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》