管灌系统巡查员智能手机App
zuoxiao
2025-01-23 b6f46408cb3dc8b01051953e5c68de6c9195db60
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package cc.shinichi.library.glide
 
import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import cc.shinichi.library.glide.cache.DataCacheKey
import cc.shinichi.library.glide.cache.SafeKeyGenerator
import cc.shinichi.library.tool.common.SLog
import com.bumptech.glide.Glide
import com.bumptech.glide.disklrucache.DiskLruCache
import com.bumptech.glide.load.engine.cache.DiskCache
import com.bumptech.glide.load.model.GlideUrl
import com.bumptech.glide.signature.EmptySignature
import java.io.File
 
/**
 * @author 工藤
 * @email qinglingou@gmail.com
 * cc.shinichi.library.glide
 * create at 2018/5/21  15:22
 * description:
 */
object ImageLoader {
 
    private const val TAG = "ImageLoader"
 
    /**
     * 获取是否有某张原图的缓存
     * 缓存模式必须是:DiskCacheStrategy.SOURCE 才能获取到缓存文件
     */
    fun getGlideCacheFile(context: Context, url: String?): File? {
        try {
            val dataCacheKey = DataCacheKey(GlideUrl(url), EmptySignature.obtain())
            val safeKeyGenerator = SafeKeyGenerator()
            val safeKey = safeKeyGenerator.getSafeKey(dataCacheKey)
            SLog.d(TAG, "safeKey = $safeKey")
            val file = File(context.cacheDir, DiskCache.Factory.DEFAULT_DISK_CACHE_DIR)
            val diskLruCache = DiskLruCache.open(file, 1, 1, DiskCache.Factory.DEFAULT_DISK_CACHE_SIZE.toLong())
            val value = diskLruCache[safeKey]
            return value?.getFile(0)
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return null
    }
 
    @JvmStatic
    fun clearMemory(activity: AppCompatActivity) {
        Glide.get(activity.applicationContext).clearMemory()
    }
 
    @JvmStatic
    fun cleanDiskCache(context: Context) {
        Thread { Glide.get(context.applicationContext).clearDiskCache() }.start()
    }
}