Android Coil3图片解码Bitmap后存入磁盘,再次加载读磁盘Bitmap缓存

Android Coil3图片解码Bitmap后存入磁盘,再次加载读磁盘Bitmap缓存

在Android中使用Coil3库实现图片缓存与加载的优化方案。通过自定义ImgCache类实现了Bitmap的磁盘缓存读写功能,包括readBmpDiskCache读取缓存和writeBmpDiskCache写入缓存方法。同时提供了decodeLargeImage方法用于处理大图解码,支持按需缩放以减少内存占用。还展示了如何配置ImageLoader,包括初始化内存缓存和磁盘缓存(各2GB容量),以及通过自定义MyImgDecoder实现图片加载流程控制。该方案通过缓存机制和按需解码优化了图片加载性能,特别适合处理大图和频繁访问图片的场景。

XML 复制代码
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_MEDIA_IMAGES" />
    <uses-permission android:name="android.permission.READ_MEDIA_VIDEO" />
Kotlin 复制代码
import android.graphics.Bitmap
import android.graphics.ImageDecoder
import android.util.Log
import java.io.BufferedOutputStream
import java.io.File
import java.io.FileOutputStream
import kotlin.math.min


object ImgCache {
    private val TAG = "fly/ImgCache"

    fun readBmpDiskCache(item: MyMedia?): Bitmap? {
        var bitmap: Bitmap? = null

        val snapShot = MyCoilMgr.INSTANCE.loader().diskCache?.openSnapshot(item.toString())
        if (snapShot != null) {
            Log.d(TAG, "命中Disk缓存 $item")

            val source = ImageDecoder.createSource(snapShot.data.toFile())
            try {
                bitmap = ImageDecoder.decodeBitmap(source)
            } catch (e: Exception) {
                Log.e(TAG, "读Disk缓存异常 $e $item")
            }
        }

        snapShot?.close()

        return bitmap
    }

    fun writeBmpDiskCache(bitmap: Bitmap?, item: MyMedia?): Any? {
        var bool = false

        if (bitmap != null) {
            val editor = MyCoilMgr.INSTANCE.loader().diskCache?.openEditor(item.toString())

            var bos: BufferedOutputStream? = null
            try {
                bos = FileOutputStream(editor?.data?.toFile()).buffered(1024 * 32)
                bitmap.compress(Bitmap.CompressFormat.PNG, 100, bos)
                bos.flush()
                bos.close()

                editor?.commit()
                Log.d(TAG, "Bitmap写入Disk缓存 $item")

                bool = true
            } catch (e: Exception) {
                Log.e(TAG, "Bitmap写Disk磁盘异常 $e")
            } finally {
                try {
                    bos?.close()
                } catch (e: Exception) {
                    Log.e(TAG, "$e $item")
                }
            }
        }

        return bool
    }


    fun decodeLargeImage(
        file: File,
        reqWidth: Int,
        reqHeight: Int
    ): Bitmap? {
        if (!file.exists() || reqWidth <= 0 || reqHeight <= 0) return null

        return try {
            val source = ImageDecoder.createSource(file)

            ImageDecoder.decodeBitmap(source) { decoder, info, _ ->
                val srcW = info.size.width
                val srcH = info.size.height

                val scale = min(
                    reqWidth.toFloat() / srcW,
                    reqHeight.toFloat() / srcH
                ).coerceAtMost(1f)

                val finalW = (srcW * scale).toInt().coerceAtLeast(1)
                val finalH = (srcH * scale).toInt().coerceAtLeast(1)

                decoder.allocator = ImageDecoder.ALLOCATOR_SOFTWARE
                decoder.memorySizePolicy = ImageDecoder.MEMORY_POLICY_LOW_RAM

                decoder.setTargetSize(finalW, finalH)
            }
        } catch (t: Throwable) {
            Log.e(TAG, "decodeLargeImage $t ${file.absolutePath} $reqWidth $reqHeight")
            null
        }
    }
}
Kotlin 复制代码
import android.os.Bundle
import android.util.Log
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import coil3.request.ImageRequest
import coil3.request.SuccessResult
import coil3.size.Scale
import coil3.toBitmap

class MainActivity : AppCompatActivity() {
    companion object {
        const val TAG = "fly/MainActivity"
        const val SIZE = 1000
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val imageView = findViewById<ImageView>(R.id.image)

        val filePath = "/sdcard/Download/data/xxx.jpg"

        val req = ImageRequest.Builder(this)
            .data(filePath)
            .size(SIZE)
            .scale(Scale.FIT)
            .decoderFactory(MyImgDecoder.Factory(MyMedia(filePath, SIZE, SIZE)))
            .listener(object : ImageRequest.Listener {
                override fun onSuccess(request: ImageRequest, result: SuccessResult) {
                    Log.d(TAG, "onSuccess ${request.data}")
                    imageView.setImageBitmap(result.image.toBitmap())
                }
            })
            .build()
        MyCoilMgr.INSTANCE.enqueue(req)
    }
}
Kotlin 复制代码
import android.app.Application

class MyApp : Application(){
    companion object {
        const val TAG = "fly/MyApp"
    }

    override fun onCreate() {
        super.onCreate()
        MyCoilMgr.INSTANCE.init(applicationContext)
    }
}
Kotlin 复制代码
import android.content.Context
import android.os.Environment
import android.util.Log
import coil3.EventListener
import coil3.ImageLoader
import coil3.decode.Decoder
import coil3.disk.DiskCache
import coil3.disk.directory
import coil3.fetch.Fetcher
import coil3.imageDecoderEnabled
import coil3.memory.MemoryCache
import coil3.request.CachePolicy
import coil3.request.ImageRequest
import coil3.request.Options
import java.io.File


class MyCoilMgr {
    companion object {

        const val TAG = "fly/MyCoilMgr"

        val INSTANCE by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) { MyCoilMgr() }
    }

    private var mImageLoader: ImageLoader? = null

    private constructor() {
        Log.d(TAG, "constructor")
    }

    fun init(ctx: Context): ImageLoader {
        if (mImageLoader != null) {
            return mImageLoader!!
        }

        Log.d(TAG, "初始化ImageLoader")

        //初始化加载器。
        mImageLoader = ImageLoader.Builder(ctx)
            .imageDecoderEnabled(true)
            .memoryCachePolicy(CachePolicy.ENABLED)
            .memoryCache(initMemoryCache())
            .diskCachePolicy(CachePolicy.ENABLED)
            .diskCache(initDiskCache())
            .eventListener(object : EventListener() {
                override fun fetchStart(request: ImageRequest, fetcher: Fetcher, options: Options) {
                    Log.d(TAG, "fetchStart ${request.data}")
                }

                override fun decodeStart(request: ImageRequest, decoder: Decoder, options: Options) {
                    Log.d(TAG, "decodeStart ${request.data}")
                }
            })
            .components {

            }.build()

        return mImageLoader!!
    }

    private fun initMemoryCache(): MemoryCache {
        //内存缓存。
        val memoryCache = MemoryCache.Builder()
            .maxSizeBytes(1024 * 1024 * 1024 * 2L) //2GB
            .build()
        return memoryCache
    }

    private fun initDiskCache(): DiskCache {
        //磁盘缓存。
        val diskCacheFolder = Environment.getExternalStorageDirectory()
        val diskCacheName = "fly_disk_cache"

        val cacheFolder = File(diskCacheFolder, diskCacheName)
        if (cacheFolder.exists()) {
            Log.d(TAG, "${cacheFolder.absolutePath} exists")
        } else {
            if (cacheFolder.mkdir()) {
                Log.d(TAG, "${cacheFolder.absolutePath} create OK")
            } else {
                Log.e(TAG, "${cacheFolder.absolutePath} create fail")
            }
        }

        val diskCache = DiskCache.Builder()
            .maxSizeBytes(1024 * 1024 * 1024 * 2L) //2GB
            .directory(cacheFolder)
            .build()

        Log.d(TAG, "cache folder = ${diskCache.directory.toFile().absolutePath}")

        return diskCache
    }


    fun enqueue(request: ImageRequest) {
        mImageLoader?.enqueue(request)
    }

    fun loader(): ImageLoader {
        return mImageLoader!!
    }
}
Kotlin 复制代码
import android.util.Log
import coil3.ImageLoader
import coil3.asImage
import coil3.decode.DecodeResult
import coil3.decode.Decoder
import coil3.fetch.SourceFetchResult
import coil3.request.Options
import java.io.File

class MyImgDecoder(val item: MyMedia) : Decoder {
    companion object {
        const val TAG = "fly/MyImgDecoder"
    }

    override suspend fun decode(): DecodeResult? {
        val t = System.currentTimeMillis()

        var bimap = ImgCache.readBmpDiskCache(item)
        if (bimap == null) {
            Log.d(TAG, "没有磁盘缓存 $item")
            bimap = ImgCache.decodeLargeImage(File(item.filePath), MainActivity.SIZE, MainActivity.SIZE)
            Log.d(TAG, "decode() bimap ${bimap?.byteCount} $item")
            ImgCache.writeBmpDiskCache(bimap, item)
        } else {
            Log.d(TAG, "命中磁盘缓存  $item")
        }

        Log.d(TAG, "time cost =  ${System.currentTimeMillis() - t}ms $item")

        return DecodeResult(image = bimap?.asImage()!!, isSampled = false)
    }

    class Factory(val media: MyMedia) : Decoder.Factory {
        override fun create(
            result: SourceFetchResult,
            options: Options,
            imageLoader: ImageLoader,
        ): Decoder {
            return MyImgDecoder(item = media)
        }
    }
}
Kotlin 复制代码
class MyMedia {
    companion object {
        const val TAG = "fly/MyMedia"
    }

    var filePath: String? = null
    var width: Int = 0
    var height: Int = 0

    constructor(filePath: String, width: Int, height: Int) {
        this.filePath = filePath
        this.width = width
        this.height = height
    }

    override fun toString(): String {
        return "MyMedia(filePath=$filePath, width=$width, height=$height)"
    }
}

相关:

https://blog.csdn.net/zhangphil/article/details/150224812

相关推荐
00后程序员张4 分钟前
无需 Mac 的 iOS App 上架工具,开心上架Appuploader 负责哪些环节
android·ios·小程序·https·uni-app·iphone·webview
吹什么轩11 小时前
MySQL:数据类型
android·adb
酷炫码神11 小时前
Day 10|Pinia 状态管理:把散落在各组件的数据,收进一个全局仓库
android·开发语言·javascript
造火箭12 小时前
安卓不同类型自动化测试介绍
android
汪海游龙15 小时前
流式回复结尾的那一下闪:逐帧定位 Compose 聊天列表的三处布局跳动
android·性能优化·kotlin
传奇开心果编程17 小时前
【Compose Multiplatform跨端声明式UI实用技术】第20课:CMP跨平台经典示例深度解析
kotlin·移动开发·跨平台·编程语言理论·开发范式·编程框架·声明式ui
2501_9159090617 小时前
Ipa Guard 防护上架,混淆、重签名和回归测试怎么做
android·ios·小程序·https·uni-app·iphone·webview
码点18 小时前
Android ANR 全解析 与 traces 分析实战
android
敲代码的瓦龙18 小时前
Jetpack?lifecycle!!!
android·kotlin
苦瓜花19 小时前
Hilt用使用及原理
android