zoukankan      html  css  js  c++  java
  • 6.8

    今日收获:

    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);
        }
    });

    我们自己另外写一个下载的线程类:

    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", "下载完毕~~~~");
    }
    }

  • 相关阅读:
    解决Shiro在Tomcat重启之后丢失登录信息
    代码安全审计大全
    解决Spring Boot打包war部署到Tomcat出现Could not open ServletContext resource
    weblogic12 重装记录
    【Spring】事物和锁及回滚异常类型
    【Spring】thymeleaf + SpringMVC局部刷新
    【Spring】@ModelAttribute三种使用场景
    Java Optional
    JAVA lambda
    最短时间(最短路+思维)
  • 原文地址:https://www.cnblogs.com/20193898liufa/p/14911621.html
Copyright © 2011-2022 走看看