Android开发okhttp下载图片带进度

Android开发okhttp下载图片带进度

下载网络图片的方法有很多,这次介绍写用okhttp来下载网络图片,主要我看中的是用okhttp下载有进度返回,提示下用户

一、思路:

用OkHttpClient().newCall(request)

二、效果图:
三、关键代码:
kotlin 复制代码
// 联系:893151960
fun download(mContext: Context,url: String,fileName:String,fileType:String, listener:(Int, Int,String) -> Unit) {
        // 需要token的时候可以这样做
        // Request request = new Request.Builder().header("token",token).url(url).build();
        val request: Request = Request.Builder().url(url).build()

        OkHttpClient().newCall(request).enqueue(object : Callback {
            override fun onFailure(call: Call, e: IOException) {
                e.printStackTrace()
                listener(0,2,"")
            }

            override fun onResponse(call: Call, response: Response) {
                listener(0,3,"")
                var inputStream: InputStream? = null
                val buf = ByteArray(2048)
                var len: Int = -1
                var fos: FileOutputStream? = null
                try {
                    inputStream = response.body?.byteStream()
                    val total: Long = response.body?.contentLength()!!
                    val file = File(getSandboxPath(mContext,fileType), fileName)
                    fos = FileOutputStream(file)
                    var sum: Long = 0
                    while ((inputStream?.read(buf) ?: -1).also { len = it } != -1) {
                        fos.write(buf, 0, len)
                        sum += len.toLong()
                        (sum * 1.0f / total * 100).toInt().let {
                            if(it > progress){
                                progress = it
                                listener(it,0,"")
                            }
                        }
                    }
                    fos.flush()
                    listener(0,1,file.path)
                } catch (e: Exception) {
                    listener(0,2,"")
                } finally {
                    try {
                        inputStream?.close()
                        fos?.close()
                    } catch (e: IOException) {
                        e.printStackTrace()
                    }
                }
            }
        })
    }
四、完整项目demo结构图:

有问题或者需要完整源码的私信我

相关推荐
奔跑吧 android1 小时前
【车载audio】【AudioPolicyManager 01】【AudioPolicyClient 类介绍】
android·audio·audioflinger·aosp15·音频开发·车载音频·audiopolicym
敲上瘾1 小时前
磁盘到 inode:深入理解 Linux ext 文件系统底层原理
android·linux·运维·文件系统
hewence11 小时前
Kotlin CoroutineScope解密
android·开发语言·kotlin
遇雪长安2 小时前
高通安卓设备DIAG端口启用指南
android·adb·usb·dm·qpst·diag·qxdm
华章酱2 小时前
MySQL EXPLAIN 完全解读:从执行计划到索引优化
android·数据库·mysql
2501_915921432 小时前
Fastlane 结合 AppUploader 来实现 CI 集成自动化上架
android·运维·ci/cd·小程序·uni-app·自动化·iphone
贤泽2 小时前
Android 15 AOSP Notification分析
android
特立独行的猫a3 小时前
腾讯Kuikly多端框架(KMP)实战:轮播图的完整实现
android·harmonyos·轮播图·jetpack compose·kuikly
2501_915921433 小时前
iOS 抓包怎么绕过 SSL Pinning 证书限制,抓取app上的包
android·网络协议·ios·小程序·uni-app·iphone·ssl
陈健平3 小时前
用 Kimi 2.5 Agent 从 0 搭建「宇宙吞噬,ps:和球球大作战这种差不多」对抗小游戏(Canvas 粒子特效 + AI Bot + 排行榜)
android·人工智能·agent·kimi2.5