文件下载工具类

Posted 嘉禾世兴

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了文件下载工具类相关的知识,希望对你有一定的参考价值。

先依赖

compile ‘com.liulishuo.filedownloader:library:1.3.0‘

 

import android.content.Context;

import com.hyphenate.chat.EMMessage;
import com.liulishuo.filedownloader.BaseDownloadTask;
import com.liulishuo.filedownloader.FileDownloadListener;
import com.liulishuo.filedownloader.FileDownloader;
import com.xuehu365.xuehu.data.DynamicDownLoadData;
import com.xuehu365.xuehu.model.DynamicModel;
import com.xuehu365.xuehu.model.event.DynamicDownLoadProgress;
import com.xuehu365.xuehu.utils.PathUtil;

import org.greenrobot.eventbus.EventBus;

import java.io.File;

/**
 * Created by Administrator on 2017/1/13.
 */
public class DynamicDownLoader {
    public static void downLoad(Context context, final DynamicModel dynamicModel) {
        String fileName = dynamicModel.getLinkUrl().substring(dynamicModel.getLinkUrl().lastIndexOf("/") + 1);
        String filePath = PathUtil.getInstance().getVideoPath() + File.separator + fileName;

        BaseDownloadTask task = FileDownloader.getImpl().create(dynamicModel.getLinkUrl()).setPath(filePath)
                .setListener(new FileDownloadListener() {
                    @Override
                    protected void pending(BaseDownloadTask task, int soFarBytes, int totalBytes) {

                    }

                    @Override
                    protected void progress(BaseDownloadTask task, int soFarBytes, int totalBytes) {
                        dynamicModel.setDownLoadPercent(Math.round(((float) (soFarBytes * 100)) / totalBytes));
                        dynamicModel.setDownLoadStatus(EMMessage.Status.INPROGRESS.ordinal());
                        DynamicDownLoadData.saveView(dynamicModel);
                        EventBus.getDefault().post(new DynamicDownLoadProgress());
                    }

                    @Override
                    protected void completed(BaseDownloadTask task) {
                        dynamicModel.setLoadlUrl(task.getPath());
                        dynamicModel.setDownLoadStatus(EMMessage.Status.SUCCESS.ordinal());
                        DynamicDownLoadData.saveView(dynamicModel);
                        EventBus.getDefault().post(new DynamicDownLoadProgress());
                    }

                    @Override
                    protected void paused(BaseDownloadTask task, int soFarBytes, int totalBytes) {

                    }

                    @Override
                    protected void error(BaseDownloadTask task, Throwable e) {
                        dynamicModel.setDownLoadStatus(EMMessage.Status.FAIL.ordinal());
                        DynamicDownLoadData.saveView(dynamicModel);
                        EventBus.getDefault().post(new DynamicDownLoadProgress());
                    }

                    @Override
                    protected void warn(BaseDownloadTask task) {

                    }
                });
        task.start();
    }

}

 

以上是关于文件下载工具类的主要内容,如果未能解决你的问题,请参考以下文章

在Android中,如何将数据从类传递到相应的布局/片段文件?

14.VisualVM使用详解15.VisualVM堆查看器使用的内存不足19.class文件--文件结构--魔数20.文件结构--常量池21.文件结构访问标志(2个字节)22.类加载机制概(代码片段

这些角度电子邮件指令代码片段如何连接

前端开发工具vscode如何快速生成代码片段

前端开发工具vscode如何快速生成代码片段

AWS S3工具类