使用WebView实现文件下载的两种方式
2015-12-21 17:30
816 查看
在应用中,通常会使用到文件下载功能,一般我们都是写一个下载操作工具类,在异步任务中执行下载功能。
今天我们来看下如何使用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);
}
});
今天我们来看下如何使用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开发案例 - 淘宝商品详情
- 设置自定义Dialog背景不变暗
- 海丽宾雅水疗服务App技术支持
- DiskLruCache解析
- 自定义Android软键盘enter键
- 「Unity」使用TexturePacker的插件TexturePackerImporter.dll 编译报错解决方案
- iOS常见的几种加密和比较
- ListView部分更新
- Android Studio自带模版:抽屉(DrawerLayout)布局
- android-ListView item的高度及分割线的长度
- 如何把App放在服务器上供用户下载
- iOS优于Android的8大直观理由
- swift禁止横屏
- Android命令Monkey压力测试,详解
- iOS开发 - 统计图的绘制
- 在Mac OS上配置Android开发环境
- Android解决按钮重复点击问题
- unity 5 资源打包控制
- No Dialect mapping for JDBC type: -1
- IOS版添加phonegap-百度社会化分享插件教程