Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Glide : get cached file location in Android/Java

I am using the Glide to display images in my app. Now I want to know the location where the Glide is storing the cached images downloaded from the urls.

I am using below code to display image.

Glide.with(mContext)
            .load(mData.get(position).getImage())
            .centerCrop()
            .override(300, 300)
            .placeholder(R.drawable.default_small)
            .diskCacheStrategy(DiskCacheStrategy.SOURCE)
            .into(holder.ivCapturedImage);
like image 491
android_griezmann Avatar asked Nov 21 '16 07:11

android_griezmann


People also ask

Where does glide cache images?

Glide will put all image resources into the memory cache by default.

Does glide cache images by default?

By default, Glide uses memory and disk caching to avoid unnecessary network calls, it checks into multiple layers of caches before initiating a new request call for an image.

What is image Manager disk cache?

A disk cache can be used in these cases to persist processed bitmaps and help decrease loading times where images are no longer available in a memory cache. Of course, fetching images from disk is slower than loading from memory and should be done in a background thread, as disk read times can be unpredictable.


2 Answers

This is one available method if you are using Glide 4.8.0 or higher

Kotlin:

val file: File = Glide.with(activity).asFile().load(url).submit().get()
val path: String = file.path

Java:

File file = Glide.with(activity).asFile().load(url).submit().get();
String path = file.getPath();

Then you can get a path looks like

/data/user/0/{package_name}/cache/image_manager_disk_cache/64c0af382f0a4b41c5dd210a3e945283d91c93b1938ee546f00b9ded701a7e40.0
like image 194
Joonsoo Avatar answered Sep 20 '22 19:09

Joonsoo


 private String getImgCachePath(String url) {
    FutureTarget<File> futureTarget = Glide.with(getBaseContext()).load(url).downloadOnly(100, 100);
    try {
        File file = futureTarget.get();
        String path = file.getAbsolutePath();
        return path;
    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (ExecutionException e) {
        e.printStackTrace();
    }
    return null;
}

the parameter url is network address of the picture,the 100,100 is the width and height of the cached picture ,you can change them according to your needs. Then,The path is the cache path.

like image 42
zhangxiaoping Avatar answered Sep 21 '22 19:09

zhangxiaoping