Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display multiple image with Glide

I want to display one image into two different image views using glide.I know that i can simply use the code below to do it.

ImageView imageView = findViewById(R.id.header_image);
Glide.with(this).load(R.drawable.header_image).into(imageView);
ImageView imageView2 = findViewById(R.id.header_image);
Glide.with(this).load(R.drawable.header_image).into(imageView2);

But it requires to load the same image twice into memory and i don't want that due to memory issues.I want to load the image once and diaplay it into two image views.What can i do to achieve it?

like image 797
Ashutosh Patoa Avatar asked Jun 02 '26 22:06

Ashutosh Patoa


2 Answers

You no need to worry about memory issue while using glide because glide has own caching system to optimize memory pls read this doc

Glide provides a number of options that allow you to choose how loads will interact with Glide’s caches on a per request basis.

Disk Cache Strategies DiskCacheStrategy can be applied with the diskCacheStrategy method to an individual request. The available strategies allow you to prevent your load from using or writing to the disk cache or choose to cache only the unmodified original data backing your load, only the transformed thumbnail produced by your load, or both.

The default strategy, AUTOMATIC, tries to use the optimal strategy for local and remote images. AUTOMATIC will store only the unmodified data backing your load when you’re loading remote data (like from URLs) because downloading remote data is expensive compared to resizing data already on disk. For local data AUTOMATIC will store the transformed thumbnail only because retrieving the original data is cheap if you need to generate a second thumbnail size or type.

To apply a DiskCacheStrategy:

GlideApp.with(fragment)
  .load(url)
  .diskCacheStrategy(DiskCacheStrategy.ALL)
  .into(imageView);

Loading only from cache In some circumstances you may want a load to fail if an image is not already in cache. To do so, you can use the onlyRetrieveFromCache method on a per request basis:

GlideApp.with(fragment)
  .load(url)
  .onlyRetrieveFromCache(true)
  .into(imageView);

If the image is found in the memory cache or in the disk cache, it will be loaded. Otherwise, if this option is set to true, the load will fail.

like image 93
Rajesh Avatar answered Jun 05 '26 10:06

Rajesh


You can use following code to load an image once and display it in multiple imageviews.

Glide.with(this)
    .asBitmap()
    .load(R.drawable.header_image)
    .into(new SimpleTarget<Bitmap>() {
        @Override
        public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
            imageview.setImageBitmap(resource);
            imageview2.setImageBitmap(resource);
        }
    });
like image 20
Zilan Pattni Avatar answered Jun 05 '26 11:06

Zilan Pattni



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!