Android Glide limits onlyRetrieveFromCache to get the memory cache submit timeout blocking method, Kotlin

Android Glide limits onlyRetrieveFromCache to get the memory cache submit timeout blocking method, Kotlin

 

import android.os.Bundle
import android.util.Log
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
import com.bumptech.glide.load.engine.DiskCacheStrategy
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.util.concurrent.TimeUnit

class MainActivity : AppCompatActivity() {
    private val TAG = "fly"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val image = findViewById<ImageView>(R.id.image)

        val size = 1000
        val timeOut = 20L //20毫秒超时
        val ctx = this

        lifecycleScope.launch(Dispatchers.IO) {
            val bmp = kotlin.runCatching {
                GlideApp.with(ctx)
                    .asBitmap()
                    .onlyRetrieveFromCache(true)
                    .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
                    .centerCrop()
                    .load(android.R.drawable.stat_notify_error)
                    .override(size)
                    .submit()
                    .get(timeOut, TimeUnit.MICROSECONDS)
            }.onSuccess {
                Log.d(TAG, "onSuccess 取到缓存")
            }.onFailure {
                Log.d(TAG, "onFailure ${it}")
            }.getOrNull()

            withContext(Dispatchers.Main) {
                if (bmp == null) {
                    Log.d(TAG, "没取到缓存,启动新任务")

                    //取不到bmp缓存,启动新加载。
                    GlideApp.with(ctx)
                        .asBitmap()
                        .centerCrop()
                        .override(size)
                        .load(android.R.drawable.stat_notify_error)
                        .into(image)
                } else {
                    Log.d(TAG, "取到缓存 ${bmp.byteCount}")
                    //取到以往存在的bmp缓存。
                    image.setImageBitmap(bmp)
                }
            }
        }
    }
}

 

 

Android Glide onlyRetrieveFromCache downloadOnly submit ,kotlin_zhangphil's blog-CSDN blog article has been viewed 353 times. [Code] Android Paging 3, kotlin (1) In actual development, although Glide solves the problem of fast loading of images, there is still an unresolved problem: such as the user's avatar, which is often read from the server. An ordinary rectangular picture, but the current design generally requires that the user avatar on the APP side be displayed as a circular avatar. So although Glide can be loaded at this time, what is loaded is a rectangle. If you want Glide_android frosted glass rounded corners. "Android image loading and caching open source framework: Android Glide" Android Glide is an open source third-party framework for image loading and caching processing. https://blog.csdn.net/zhangphil/article/details/131774130 Android Glide synchronous blocking method submit to obtain Bitmap, kotlin_glide obtains bitmap synchronously - CSDN blog article has been viewed 638 times. [Code] Android Paging 3, kotlin (1) In actual development, although Glide solves the problem of fast loading of images, there is still an unresolved problem: such as the user's avatar, which is often read from the server. An ordinary rectangular picture, but the current design generally requires that the user avatar on the APP side be displayed as a circular avatar. So although Glide can be loaded at this time, what is loaded is a rectangle. If you want Glide_android frosted glass rounded corners. "Android image loading and caching open source framework: Android Glide" Android Glide is an open source third-party framework for image loading and caching processing. _glide synchronously obtains bitmap https://blog.csdn.net/zhangphil/article/details/131641086 Android Glide determines whether the image resource is cached onlyRetrieveFromCache, using cached data, the Kotlin-CSDN blog article has been viewed 246 times. [Code] Android Paging 3, kotlin (1) In actual development, although Glide solves the problem of fast loading of images, there is still an unresolved problem: such as the user's avatar, which is often read from the server. An ordinary rectangular picture, but the current design generally requires that the user avatar on the APP side be displayed as a circular avatar. So although Glide can be loaded at this time, what is loaded is a rectangle. If you want Glide_android frosted glass rounded corners. "Android image loading and caching open source framework: Android Glide" Android Glide is an open source third-party framework for image loading and caching processing. https://blog.csdn.net/zhangphil/article/details/134038421 Kotlin exception handling runCatching, getOrDefault, getOrNull run (2) - CSDN blog article has been read 44 times. b-catch: java.lang.RuntimeException: An exception occurred in b. Kotlin exception handling try-catch-finally. Kotlin exception handling try-catch-finally_zhangphil's blog-CSDN blog. b-catch: java.lang.RuntimeException: An exception occurred in b. Kotlin exception handling try-catch-finally. Kotlin exception handling try-catch-finally_zhangphil's blog-CSDN blog. https://blog.csdn.net/zhangphil/article/details/133279933 Kotlin exception handling runCatching, getOrNull, onFailure, onSuccess (1) - CSDN blog article has been read 451 times. b-catch: java.lang.RuntimeException: An exception occurred in b. Kotlin exception handling try-catch-finally. Kotlin exception handling try-catch-finally_zhangphil's blog-CSDN blog. https://blog.csdn.net/zhangphil/article/details/133279853

 

Guess you like

Origin blog.csdn.net/zhangphil/article/details/134051794