Android高能下载库FileDownloader

我们的App中可能会提供给用户下载文件或者图片的场景,然后你可能就会考虑以下名词了

多任务下载
多线程下载
断点续传
高并发

没错,如果你自己手写下载库的话需要考虑这四个名词,接下来我们学习一下FileDownloader库,该库的作者对这四点已经封装的很好了,5000人Star的开源库你怎可错过呢?

老规矩,引用别人的库的时候一定要放别人的github地址
FileDownloader Github地址

1 引用导入

compile 'com.liulishuo.filedownloader:library:1.6.4'//最新版本见github

2 全局初始化


public class APP extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
       FileDownloader.setup(this);//注意作者已经不建议使用init方法
    }
}

3 具体调用

注意这里的path是文件名而不是文件夹的名字

注意这里的path是文件名而不是文件夹的名字

注意这里的path是文件名而不是文件夹的名字

 FileDownloader.getImpl().create(url).setWifiRequired(true).setPath(path).setListener(new FileDownloadListener() {
            @Override
            protected void pending(BaseDownloadTask task, int soFarBytes, int totalBytes) {

            }

            @Override
            protected void progress(BaseDownloadTask task, int soFarBytes, int totalBytes) {
                int percent=(int) ((double) soFarBytes / (double) totalBytes * 100);
                textView.setText("("+percent+"%"+")");
            }

            @Override
            protected void blockComplete(BaseDownloadTask task) {

            }

            @Override
            protected void completed(BaseDownloadTask task) {
                Toast.makeText(MainActivity.this,"下载完成!",Toast.LENGTH_SHORT).show();
                textView.setText("("+"100%"+")");
            }

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

            }

            @Override
            protected void error(BaseDownloadTask task, Throwable e) {

            }

            @Override
            protected void warn(BaseDownloadTask task) {
                continueDownLoad(task);//如果存在了相同的任务,那么就继续下载
            }
        }).start();
 private void continueDownLoad(BaseDownloadTask task) {
        while (task.getSmallFileSoFarBytes()!=task.getSmallFileTotalBytes()){
            int percent=(int) ((double) task.getSmallFileSoFarBytes() / (double) task.getSmallFileTotalBytes() * 100);
            textView.setText("("+percent+"%"+")");
        }
    }

没错,就是这么简单好用!这个库远比我的demo要强大,我只是展示了他的基础用法,如果用户有自己特殊的需求还请移步到github上仔细阅读源码哈~

  • 5
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
以下是一个简单的远程下载并加载动态链接的示例代码,包括下载器部分。请注意,这只是一个示例代码,仅供参考。 下载器部分: ```java public class Downloader { public static void download(String url, String localPath) throws Exception { URLConnection connection = new URL(url).openConnection(); int contentLength = connection.getContentLength(); InputStream input = new BufferedInputStream(connection.getInputStream()); OutputStream output = new FileOutputStream(localPath); byte[] data = new byte[1024]; int count; int progress = 0; while ((count = input.read(data)) != -1) { output.write(data, 0, count); progress += count; System.out.println("Downloaded " + progress + "/" + contentLength + " bytes"); } output.flush(); output.close(); input.close(); } } ``` 加载动态链接部分: ```java public class NativeLibraryLoader { private static final String LIB_NAME = "mylibrary"; private static final String LIB_URL = "https://example.com/mylibrary.so"; static { try { File tempDir = new File(System.getProperty("java.io.tmpdir")); File libFile = new File(tempDir, LIB_NAME); if (!libFile.exists()) { System.out.println("Downloading library..."); Downloader.download(LIB_URL, libFile.getAbsolutePath()); } System.load(libFile.getAbsolutePath()); } catch (Exception e) { e.printStackTrace(); } } public static native void myNativeMethod(); } ``` 在这个示例中,我们首先定义了要下载的动态链接的名称和远程URL。在加载本地之前,我们检查本地目录中是否已经有了这个,如果没有,就使用`Downloader`类从远程URL下载它。然后我们使用`System.load()`方法加载本地。 请注意,为了使本地能够正确加载,你需要确保它与Android设备的CPU架构相匹配。例如,如果你的设备使用ARM架构,则需要下载和加载ARM版本的。 当然,还有很多其他的细节需要考虑,例如错误处理、下载进度更新、的存储位置等等,但这个示例应该可以为你提供一个大致的思路。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值