Retrieving URLs associated with cached images in Glide for Android

29 views Asked by At

I'm working on an Android application where I use Glide for image loading and caching. I need to retrieve the URLs associated with the cached images stored by Glide in order to perform further operations based on those URLs.

Here's what I've tried so far:

Attempted to directly extract URLs from Glide's cache keys, but found that the cache keys don't contain this information. Explored accessing the disk cache directory to iterate through cached image files, but encountered challenges in associating these files with their corresponding URLs. Given the above, I'm looking for guidance on how to effectively retrieve the URLs associated with the cached images stored by Glide. Is there a recommended approach or best practice for achieving this? Any insights or suggestions would be greatly appreciated.

please see this:

fun getAllCachedImages(context: Context) {
        val glide = Glide.with(context)
        val cacheDirectory = Glide.getPhotoCacheDir(context)

        val cachedFiles = cacheDirectory?.listFiles()
        if (cachedFiles != null) {
            for (file in cachedFiles) {

                val options: RequestOptions =
                    RequestOptions().diskCacheStrategy(DiskCacheStrategy.ALL)
                val requestBuilder: RequestBuilder<Drawable> = glide.load(file).apply(options)
                val cacheKey: Key = requestBuilder.signature
                //it return EmptySignature


                val cacheKey2 = Glide.with(context)
                    .load(Uri.fromFile(file))
                    .downloadOnly(com.bumptech.glide.request.target.Target.SIZE_ORIGINAL, com.bumptech.glide.request.target.Target.SIZE_ORIGINAL)
                    .get()
                    .toString()
                
                //this return something like this:
                // -> /data/user/0/MY_APP_PACKAGE_NAME/cache/image_manager_disk_cache/dac01c2968b075a386bcc7b8bcc4d0433e1225ae82264fb4cfc1740645e435b4.0


            }
        }
    }

I'm using Glide version 4.12.0.

My goal is to associate each cached image with its original URL for further processing within my application.

In this Page, it mentioned that all cache keys contain File, Uri, and Url but I don't know how to retrieve

Any code examples or pointers to relevant documentation would be immensely helpful.

0

There are 0 answers