Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preload multiple images with Glide

We are trying to preload images into cache memory to load them later (the images are located in the Asset folder of the application)

What we tried:

Glide.with(this)     .load(pictureUri)     .diskCacheStrategy(DiskCacheStrategy.ALL);  Glide.with(this)     .load(picture_uri)     .diskCacheStrategy(DiskCacheStrategy.ALL)     .preload(); 

The issue: Images are cached only when we are trying to load/display them: They have to be loaded in memory before so that they appear faster.

Glide.with(this)     .load(picture_uri)     .into(imageView); 

We also tried to use a GlideModule to increase the CacheMemory size:

public class GlideModule implements com.bumptech.glide.module.GlideModule {     @Override     public void applyOptions(Context context, GlideBuilder         builder.setMemoryCache(new LruResourceCache(100000));     }      @Override     public void registerComponents(Context context, Glide glide) {     } } 

In the manifest:

 <meta-data android:name=".GlideModule" android:value="GlideModule"/> 

Nothing is working so far. Any idea?


We trying to use an invisible 1 dp imageView, but the result is the same:

for(Drawing drawing: getDrawingsForTab(tab)){      Glide.with(this)             .load(drawing.getImage().toUri())             .dontAnimate()             .diskCacheStrategy(DiskCacheStrategy.ALL)             .into(mPreloadCacheIv);      for(Picture picture : getPictures()){          Glide.with(this)                 .load(picture.getPicture().toUri())                 .dontAnimate()                 .diskCacheStrategy(DiskCacheStrategy.ALL)                 .into(mPreloadCacheIv);     } } 
like image 402
An-droid Avatar asked Jun 22 '16 09:06

An-droid


People also ask

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.

Can we preload images?

To preload responsive images, new attributes were recently added to the <link> element: imagesrcset and imagesizes . They are used with <link rel="preload"> and match the srcset and sizes syntax used in <img> element. This kicks off a request using the same resource selection logic that srcset and sizes will apply.


1 Answers

Use the following code to cache images without displaying them

  1. using the downloadOnly method if you are looking to download images from the web and store them in the diskCache:

    FutureTarget<File> future = Glide.with(applicationContext)     .load(yourUrl)     .downloadOnly(500, 500); 
  2. using preload method if you want to load them into the Memory Cache.

    Glide.with(context)         .load(url)         .preload(500, 500); 

You can later use the cached images using

Glide.with(yourFragment)     .load(yourUrl)     .into(yourView); 

like image 142
Vinay W Avatar answered Sep 20 '22 19:09

Vinay W