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

相关推荐
YM52e2 小时前
鸿蒙Flutter Center居中组件:Align对齐详解
android·学习·flutter·华为·harmonyos·鸿蒙
时间的拾荒人3 小时前
MySQL 视图详解
android·数据库·mysql
祉猷并茂,雯华若锦3 小时前
Appium 3.x安卓按键与通知栏操作全指南
android·appium
码农coding4 小时前
android 12 SurfaceFlinger开机启动分析
android
hunterandroid4 小时前
Paging 3 RemoteMediator 实战:构建离线优先的分页列表
android·前端
码农coding4 小时前
android12 开机启动PMS
android
Days20504 小时前
GPT-Image-2 国风美学人设生成提示词分享
android·gpt
浮江雾5 小时前
Flutter第十节-----Flutter布局与组件全解析
android·开发语言·前端·学习·flutter·入门
Jomurphys5 小时前
Compose 适配 - 自适应布局(窗口大小类 WindowSizeClasses、列表详情、辅助窗格)
android·compose
施棠海5 小时前
Pixel2XML实战:10分钟从设计稿做出一个能拨号的Android拨号盘页面(附完整业务代码)
android·java·ui