Android通过glide和rxjava实现图片下载

首先来看实现方式:

public void download(View view) {
        List baseBeanList = new ArrayList<>();
        for (int i = 0;i < paths.length; i++){
            BaseBean baseBean = new BaseBean();
            baseBean.setUrl(paths[i]);
            baseBean.setFilename("picture" + i);
            baseBeanList.add(baseBean);
        }
        new DownloadPicture.DownloadPictureBuilder(baseBeanList)
                .setLocalDir("/0000/")
                .setPictureFormat(PNG)
                .build()
                .download(MainActivity.this, new IDownload() {
            @Override
            public void start() {
                Log.i("download","start");
            }

            @Override
            public void progress(int progress) {
                Toast.makeText(MainActivity.this,"当前进度为:" + progress,Toast.LENGTH_SHORT).show();
                Log.i("download","当前进度为:" + progress);
            }

            @Override
            public void complete() {
                Toast.makeText(MainActivity.this,"下载完成",Toast.LENGTH_SHORT).show();
                Log.i("download","complete");
            }

            @Override
            public void fail(BaseBean baseBean) {
                Toast.makeText(MainActivity.this,"下载失败" + baseBean.getFilename(),Toast.LENGTH_SHORT).show();
                Log.i("download","当前失败图片为:" + baseBean.getFilename());
            }

            @Override
            public void end() {
                Log.i("download","end");
            }

            @Override
            public void error(Exception e) {
                Log.i("download","下载异常:" + e.getMessage());
            }
        });
    }

首先你的有个filelist,这个是必须的参数

然后可以set本地目录,set图片存储格式,然后IDownload回调

该下载内部已包含本地目录创建的命名正则匹配,不符合用空字符串替换

也可通过该方式学习下简单建造者模式的实现

代码就直接GitHub见吧 https://github.com/sdgSnow/DownloadPicture

 

你可能感兴趣的:(技术,设计模式,分享)