glide icon indicating copy to clipboard operation
glide copied to clipboard

How to use memory caching when loading Drawable resources?

Open KaiXuan666 opened this issue 2 years ago • 1 comments
trafficstars

My current requirement is to display the icons of installed applications in a list and convert them into rounded corners.

public static Drawable getAppIcon(final String packageName) { if (UtilsBridge.isSpace(packageName)) return null; try { PackageManager pm = Utils.getApp().getPackageManager(); PackageInfo pi = pm.getPackageInfo(packageName, 0); return pi == null ? null : pi.applicationInfo.loadIcon(pm); } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); return null; } } Currently, I use this method to get the icon Drawable for each item in the list and then use Glide's load(Drawable) method. However, there is a problem because the EngineKey compares Drawable objects using reference equality, and my Drawable objects are always new instances, so they cannot hit the cache. I tried using requestBuilder.signature(new ObjectKey(pkgName)), but it doesn't work because the model objects are not equal, and the ObjectKey has no effect.

How can I make Drawable objects hit the memory cache? Do I need to store Drawable objects for each application package name myself? Will this increase memory usage? And when should I release them?

KaiXuan666 avatar Jun 03 '23 05:06 KaiXuan666