美文网首页
Android如何下载文件

Android如何下载文件

作者: 念念不忘_2016 | 来源:发表于2023-08-05 23:14 被阅读0次

    OkDownload 进行下载

    fun downloadPic(userId: String?, url: String, parentFile: File) {
        val task: DownloadTask = DownloadTask.Builder(url, parentFile)
            .setFilename(userId) // the minimal interval millisecond for callback progress
            .setMinIntervalMillisCallbackProcess(30) // do re-download even if the task has already been completed in the past.
            .setPassIfAlreadyCompleted(false)
            .build()
     // execute task synchronized
        task.execute(object : DownloadListener2() {
            override fun taskStart(task: DownloadTask) {
                LogUtils.v("FaceManager", "开始下载")
            }
    
            override fun taskEnd(task: DownloadTask, cause: EndCause, realCause: Exception?) {
                LogUtils.v("FaceManager", "下载结束" + task.file?.path ?: "")
               
            }
        })
    }
    

    Okgo 下载

    public void downloadFile(String url, String fileName) {
        OkGo.<File>get(url)
                .tag(this)
                .execute(new FileCallback("文件路径", fileName) {
                    @Override
                    public void onStart(Request<File, ? extends Request> request) {
                        super.onStart(request);
    
                    }
    
                    @Override
                    public void onSuccess(Response<File> response) {
    
                    }
    
                    @Override
                    public void onError(Response<File> response) {
                        super.onError(response);
    
                    }
                });
    }

    相关文章

      网友评论

          本文标题:Android如何下载文件

          本文链接:https://www.haomeiwen.com/subject/hpeipdtx.html