• 2021/5/20


    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);
        }
    });
    我们自己另外写一个下载的线程类:
    
    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", "下载完毕~~~~");
        }
    }



    2.遇到的问题
    文件缓存

    3.明天目标

  • 相关阅读:
    正则表达式把所有Paul替换成Ringo:Paul Puala Pualine paul Paul
    DOM 和 BOM
    新手的grid布局
    css中的单位和css中的颜色表示方法
    css定位
    Winform 通过 WebBrowser 与 JS 交互
    PDF目录编辑器使用介绍
    [.NET] 控制只启动单个指定外部程序
    搭建 Frp 来远程内网 Windows 和 Linux 机子
    CentOs8 nmcli命令行方式操作网卡配置
  • 原文地址:https://www.cnblogs.com/qiangini/p/14909325.html
Copyright © 2020-2023  润新知