使用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);

}

});

你可能感兴趣的:(android,Android开发,webView,a)