19

When I use glide, some images doesn't loads. How can I store it in the cache, or anywhere, to when I'll use the app all my images loads. Example picture of my problem:

screenshot

My code:

.java

             home_ib7_monster_truck = 
            (ImageButton)findViewById(R.id.home_ib7_monster_truck);
             Glide.with(Home.this)
            .load(R.drawable.moviep_monster_truck)
            .centerCrop()
            .fitCenter()
            .diskCacheStrategy(DiskCacheStrategy.ALL)
            .into(home_ib7_monster_truck);

.xml

<ImageButton
            android:id="@+id/home_ib7_monster_truck"
            android:layout_width="98dp"
            android:layout_height="155dp"
            android:layout_alignStart="@+id/home_ib4_keplers_dream"
            android:layout_below="@+id/home_ib4_keplers_dream"
            android:layout_marginTop="14dp"
            android:background="@android:color/transparent"
            android:scaleType="fitCenter" />

I use glide 'cause I saw it's easy to use, and I can load images from drawables. And the main problem is, that it do this randomly, so I mean once all my images are doesn't seems, another time all images can see, and I don't know why. I use 980x1550 sized images, 'casuse I don't want my images beign full of pixels, and when I use another method like src in the .xml I got memory error. So anyone know any soluton, how can I cache the images?

EDIT: With these (.diskCacheStrategy(DiskCacheStrategy.RESULT ; diskCacheStrategy(DiskCacheStrategy.Source) I have the same problem. I get this from LogCat: Throwing OutOfMemoryError "Failed to allocate a 1519012 byte allocation with 230112 free bytes and 224KB until OOM" and i don't know how, when it should be cached.

1
  • Post the Glide logs so that we can to know why the image loading is failing
    – Pulak
    Jan 8, 2018 at 14:09

3 Answers 3

24

Please Read the Description below as per official documentation : https://futurestud.io/tutorials/glide-caching-basics

enter image description here

enter image description here

3
  • The 4.x after the Glide what means? Jan 8, 2018 at 14:33
  • 3
    version 4.x is version of library like 4.5.0 Jan 8, 2018 at 14:35
  • Does it matter which one I use if I just want Glide to use the cache when needed?
    – MikkelT
    Nov 9, 2021 at 12:50
5

Here is a common scenario you want to load image from the cache and download the new data only if a new image exist , you have to have a way to know if the image is updated ,for example a variable called imageTimeStamp containing the last time the image was updated.

            Glide.with(context).load(url).apply(new RequestOptions()
                .placeholder(R.drawable.defultIcon)
                .signature(new ObjectKey(image.getPPTimeStamp()))) // here you add some value , if the next time you add the same value then it will load from cache otherwise if you put new value you will download , then save in cache
                .into(icon);
1

Glide will put all image resources into the memory cache by default. Thus, a specific call .skipMemoryCache( false ) is not necessary.

Hint: beware of the fact, that if you make an initial request to the same URL without the .skipMemoryCache( true ) and then with the method, the resource will get cached in memory. Make sure you're consistent across all calls to the same resource, when you want to adjust the caching behavior!

Referance: https://futurestud.io/tutorials/glide-caching-basics

1
  • With these (.diskCacheStrategy(DiskCacheStrategy.RESULT ; diskCacheStrategy(DiskCacheStrategy.Source) I have the same problem. I get this from LogCat: Throwing OutOfMemoryError "Failed to allocate a 1519012 byte allocation with 230112 free bytes and 224KB until OOM" and i don't know how, when it should be cached. Jan 8, 2018 at 17:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.