使用WebView实现文件下载的两种方式

在应用中,通常会使用到文件下载功能,一般我们都是写一个下载操作工具类,在异步任务中执行下载功能。 今天我们来看下如何使用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);

}

});

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值