11

Glide获取图片缓存文件名Key

 3 years ago
source link: https://renyugang.blog.csdn.net/article/details/103929072
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Glide获取图片缓存文件名Key

胖虎 2020-01-10 18:30:12 2552
分类专栏: Android知识点

今天同事碰到一个问题,他的由于项目中用了Glide图片加载框架,而我们的图片Url又有进行CDN裁剪,他的项目中有小图,中图,大图,三种分辨率的图片,发现当有了大图后,小图去加载时,由于url中带了尺寸做CDN图片裁剪,如: http://xxxxxx.jpg?xxxxxx_120_120.jpg,这样的话,Glide生成的缓存path就不一样,导致多次重新拉取小图。

解决问题最好的方法就是清楚问题后,再找代码了。
这个问题转换为:获取url到缓存文件path的生成规则算法上。
找到Glide最终生成path的路径为:/data/data/your_packagexxxxxxx/cache/image_manager_disk_cache
找到对应的生成规则:com.bumptech.glide.load.engine.EngineKey#updateDiskCacheKey

具体阅读代码的过程不累赘,直接把获取函数贴出来哈。

Glide 3.+版本

/**
     * Glide缓存存储路径:/data/data/your_packagexxxxxxx/cache/image_manager_disk_cache
     * Glide文件名生成规则函数 : 3.0+ 版本
     *
     * @autor 胖虎 https://blog.csdn.net/ljphhj
     *
     * @param url 传入您的图片地址url
     * @param width 设备屏幕分辨率的宽度 eg : 1080
     * @param height 设备屏幕分辨率的高度 eg : 1920
     * @return
     */
    private String getGlide3_SafeKey(String url, int width, int height){
        byte[] dimensions = ByteBuffer.allocate(8)
                .putInt(width)
                .putInt(height)
                .array();
        try {
            MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");

            EmptySignature signature = EmptySignature.obtain();
            signature.updateDiskCacheKey(messageDigest);
            messageDigest.update(url.getBytes(STRING_CHARSET_NAME));
            messageDigest.update(dimensions);
            messageDigest.update("".getBytes(STRING_CHARSET_NAME));
            messageDigest.update("ImageVideoBitmapDecoder.com.bumptech.glide.load.resource.bitmap".getBytes(STRING_CHARSET_NAME));
            messageDigest.update("FitCenter.com.bumptech.glide.load.resource.bitmap".getBytes(STRING_CHARSET_NAME));
            messageDigest.update("BitmapEncoder.com.bumptech.glide.load.resource.bitmap".getBytes(STRING_CHARSET_NAME));
            messageDigest.update("".getBytes(STRING_CHARSET_NAME));

            String safeKey = Util.sha256BytesToHex(messageDigest.digest());
            return safeKey + ".0";
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return null;
    }

Glide 4.+版本

    /**
     * Glide缓存存储路径:/data/data/your_packagexxxxxxx/cache/image_manager_disk_cache
     * Glide文件名生成规则函数 : 4.0+ 版本
     *
     * @autor 胖虎 https://blog.csdn.net/ljphhj
     *
     * @param url 传入您的图片地址url
     * @return
     */
    private String getGlide4_SafeKey(String url) {
        try {
            MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");
            EmptySignature signature = EmptySignature.obtain();
            signature.updateDiskCacheKey(messageDigest);
            new GlideUrl(url).updateDiskCacheKey(messageDigest);
            String safeKey = Util.sha256BytesToHex(messageDigest.digest());
            return safeKey + ".0";
        } catch (Exception e) {
        }
        return null;
    }

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK