本节引言
1.调用其它浏览器下载文件:
关键代码如下:
wView.setDownloadListener(new DownloadListener(){ @Override public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) { Log.e("HEHE","开始下载"); Uri uri = Uri.parse(url); Intent intent = new Intent(Intent.ACTION_VIEW,uri); startActivity(intent); } });
如果你手机内存在多个浏览器的话,会打开一个对话框供你选择其中一个浏览器进行下载~
2.自己写线程下载文件
核心代码:
我们自己另外写一个下载的线程类:
DownLoadThread.java
/** * Created by Jay on 2015/9/14 0014. */ public class DownLoadThread implements Runnable { private String dlUrl; public DownLoadThread(String dlUrl) { this.dlUrl = dlUrl; } @Override public void run() { Log.e("HEHE", "开始下载~~~~~"); 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)) { Log.e("HEHE","SD卡可写"); downloadFile = Environment.getExternalStorageDirectory(); sdFile = new File(downloadFile, "csdn_client.apk"); fout = new FileOutputStream(sdFile); }else{ Log.e("HEHE","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(); } } } Log.e("HEHE", "下载完毕~~~~"); } }
然后MainActivity.java中创建并启动该线程:
wView.setDownloadListener(new DownloadListener(){ @Override public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) { Log.e("HEHE","onDownloadStart被调用:下载链接:" + url); new Thread(new DownLoadThread(url)).start(); } });
运行结果:
我们打开SD卡可以看到,下载好的文件已经安安静静地躺在SD卡里了:
注意事项:
好的,另外,别忘了写SD卡的读写权限以及Internet访问网络的权限:
<uses-permission android:name="android.permission.INTERNET"/> <!-- 在SDCard中创建与删除文件权限 --> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/> <!-- 往SDCard写入数据权限 --> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
还有,in = conn.getInputStream();要写在conn设置完所有东西的后面!!切记,不然什么都读不了!