bumptech / glide

An image loading and caching library for Android focused on smooth scrolling
https://bumptech.github.io/glide/
Other
34.62k stars 6.12k forks source link

Nested thumbnail doesn't work #1687

Closed MMarco94 closed 6 years ago

MMarco94 commented 7 years ago

Glide Version: 3.7.0

Integration libraries: No OkHttp3 nor Volley

Device/Android Version:OnePlus 3

Hi. I've got a list of different URLs for the same image, with different sizes. Using a custom BaseGlideUrlLoader, I'm loading the image which resolution is closer to the resolution of the ImageView. This works flawlessly.

However, I'm trying to achieve the following: I want to use as thumbnail whatever image I've got in RAM/disk. To do so I use the following code:

final DrawableRequestBuilder<Image> drawableRequestBuilder = Glide.with(context)
        .using(new ImageGlideLoader(context)) //The BaseGlideUrlLoader which provides the image which size is closer to the ImageView's size 
        .load(image)
        .thumbnail(getThumbnailLoader(context, image))
        .diskCacheStrategy(DiskCacheStrategy.SOURCE)
        .into(imageView);

...

public static GenericRequestBuilder<?, ?, ?, GlideDrawable> getThumbnailLoader(Context context, Image image) {
    final ArrayList<ImageSource> imageSources = new ArrayList<>(image.getSources());
    Collections.sort(imageSources, new ImageSourceSizeComparator()); //Ordered by size ascending

    //The GenericRequestBuilder for the smaller image. Allow network downloads     
    GenericRequestBuilder<?, ?, ?, GlideDrawable> ret = Glide.with(context)
        .load(imageSources.get(0).getUrl())
        .diskCacheStrategy(DiskCacheStrategy.SOURCE);
    for (int i = 1; i < imageSources.size(); i++) {
        ret = Glide.with(context)
            .using(new NetworkDisablingLoader()) //Block network downloads
            .load(imageSources.get(i).getUrl())
            .thumbnail(ret);
    }
    return ret;
}

Basically, getThumbnailLoader builds a GenericRequestBuilder with the image in the highest resolution, which thumbnail is the second image with higher resolution and so on, until the smaller image.

However, using this code, it's always displayed the image in lower resolution, which is strange, since the url that my ImageGlideLoader returns is correct (it returns in fact the url which bet approximate the size of the ImageView). I've tried to simplify the code and use only the smaller image as thumbnail, without nesting, and it works, but I need to try to use all the sizes, since it's more likely to have a cache hit.

Is there a problem with nested thumbnails? Am I doing something wrong? I'm attaching the log that the library outputs during this process.

Stack trace / LogCat:

Started new load in 0.043802ms, key: EngineKey{https://image.tmdb.org/t/p/w500/fWwcHPT3dhY8kSUyJ0WZhqUpvX4.jpg+com.bumptech.glide.signature.EmptySignature@9ed4b45+[432x648]+''+'ImageVideoBitmapDecoder.com.bumptech.glide.load.resource.bitmap'+''+'BitmapEncoder.com.bumptech.glide.load.resource.bitmap'+'GifBitmapWrapperDrawableTranscoder.com.bumptech.glide.load.resource.transcode'+''}
Transcoded transformed from cache in 0.028697999999999998, key: EngineKey{https://image.tmdb.org/t/p/w154/fWwcHPT3dhY8kSUyJ0WZhqUpvX4.jpg+com.bumptech.glide.signature.EmptySignature@9ed4b45+[432x648]+''+'ImageVideoBitmapDecoder.com.bumptech.glide.load.resource.bitmap'+''+'BitmapEncoder.com.bumptech.glide.load.resource.bitmap'+'GifBitmapWrapperDrawableTranscoder.com.bumptech.glide.load.resource.transcode'+''}
Decoded transformed from cache in 0.028905999999999998, key: EngineKey{https://image.tmdb.org/t/p/w185/fWwcHPT3dhY8kSUyJ0WZhqUpvX4.jpg+com.bumptech.glide.signature.EmptySignature@9ed4b45+[432x648]+''+'ImageVideoBitmapDecoder.com.bumptech.glide.load.resource.bitmap'+''+'BitmapEncoder.com.bumptech.glide.load.resource.bitmap'+'GifBitmapWrapperDrawableTranscoder.com.bumptech.glide.load.resource.transcode'+''}
Decoded transformed from cache in 0.022552, key: EngineKey{https://image.tmdb.org/t/p/w342/fWwcHPT3dhY8kSUyJ0WZhqUpvX4.jpg+com.bumptech.glide.signature.EmptySignature@9ed4b45+[432x648]+''+'ImageVideoBitmapDecoder.com.bumptech.glide.load.resource.bitmap'+''+'BitmapEncoder.com.bumptech.glide.load.resource.bitmap'+'GifBitmapWrapperDrawableTranscoder.com.bumptech.glide.load.resource.transcode'+''}
Added to existing load in 0.02974ms, key: EngineKey{https://image.tmdb.org/t/p/w500/fWwcHPT3dhY8kSUyJ0WZhqUpvX4.jpg+com.bumptech.glide.signature.EmptySignature@9ed4b45+[432x648]+''+'ImageVideoBitmapDecoder.com.bumptech.glide.load.resource.bitmap'+''+'BitmapEncoder.com.bumptech.glide.load.resource.bitmap'+'GifBitmapWrapperDrawableTranscoder.com.bumptech.glide.load.resource.transcode'+''}
Transcoded transformed from cache in 0.09948, key: EngineKey{https://image.tmdb.org/t/p/w342/fWwcHPT3dhY8kSUyJ0WZhqUpvX4.jpg+com.bumptech.glide.signature.EmptySignature@9ed4b45+[432x648]+''+'ImageVideoBitmapDecoder.com.bumptech.glide.load.resource.bitmap'+''+'BitmapEncoder.com.bumptech.glide.load.resource.bitmap'+'GifBitmapWrapperDrawableTranscoder.com.bumptech.glide.load.resource.transcode'+''}
Transcoded transformed from cache in 0.031771, key: EngineKey{https://image.tmdb.org/t/p/w500/fWwcHPT3dhY8kSUyJ0WZhqUpvX4.jpg+com.bumptech.glide.signature.EmptySignature@9ed4b45+[432x648]+''+'ImageVideoBitmapDecoder.com.bumptech.glide.load.resource.bitmap'+''+'BitmapEncoder.com.bumptech.glide.load.resource.bitmap'+'GifBitmapWrapperDrawableTranscoder.com.bumptech.glide.load.resource.transcode'+''}
Decoded transformed from cache in 0.070156, key: EngineKey{https://image.tmdb.org/t/p/w780/fWwcHPT3dhY8kSUyJ0WZhqUpvX4.jpg+com.bumptech.glide.signature.EmptySignature@9ed4b45+[432x648]+''+'ImageVideoBitmapDecoder.com.bumptech.glide.load.resource.bitmap'+''+'BitmapEncoder.com.bumptech.glide.load.resource.bitmap'+'GifBitmapWrapperDrawableTranscoder.com.bumptech.glide.load.resource.transcode'+''}
Transcoded transformed from cache in 0.032187, key: EngineKey{https://image.tmdb.org/t/p/w780/fWwcHPT3dhY8kSUyJ0WZhqUpvX4.jpg+com.bumptech.glide.signature.EmptySignature@9ed4b45+[432x648]+''+'ImageVideoBitmapDecoder.com.bumptech.glide.load.resource.bitmap'+''+'BitmapEncoder.com.bumptech.glide.load.resource.bitmap'+'GifBitmapWrapperDrawableTranscoder.com.bumptech.glide.load.resource.transcode'+''}
Decoded transformed from cache in 0.055989, key: EngineKey{https://image.tmdb.org/t/p/original/fWwcHPT3dhY8kSUyJ0WZhqUpvX4.jpg+com.bumptech.glide.signature.EmptySignature@9ed4b45+[432x648]+''+'ImageVideoBitmapDecoder.com.bumptech.glide.load.resource.bitmap'+''+'BitmapEncoder.com.bumptech.glide.load.resource.bitmap'+'GifBitmapWrapperDrawableTranscoder.com.bumptech.glide.load.resource.transcode'+''}
Transcoded transformed from cache in 0.026198, key: EngineKey{https://image.tmdb.org/t/p/original/fWwcHPT3dhY8kSUyJ0WZhqUpvX4.jpg+com.bumptech.glide.signature.EmptySignature@9ed4b45+[432x648]+''+'ImageVideoBitmapDecoder.com.bumptech.glide.load.resource.bitmap'+''+'BitmapEncoder.com.bumptech.glide.load.resource.bitmap'+'GifBitmapWrapperDrawableTranscoder.com.bumptech.glide.load.resource.transcode'+''}
TWiStErRob commented 7 years ago

I think you're missing the diskCacheStrategy in the loop. The logs say that it's using RESULT cache, I don't think that's what you want.

If these are the only Glide lines dealing with these images, then no-one will populate the RESULT cache (default) ever. Only the original and smallest images will be SOURCE cached. You have to specify the .diskCacheStrategy for both read and write operations together.

MMarco94 commented 7 years ago

Well, you're right, but unfortunately this doesn't solve the issue.

MMarco94 commented 7 years ago

Further investigation revealed that the issue appears only if the scaleType of the image is matrix. This is still a problem since I need to use that scaleType.

TWiStErRob commented 7 years ago

Hmm, that's interesting because I don't see any transformations in the EngineKeys. Try forcing .dontTransform() on all loads, thumbnail or not. Since you're loading the nearest size, you'll probably hit the sizing near inSampleSize == 1 and any transformation would just slow loads down.

MMarco94 commented 7 years ago

Using .dontTransform() makes things worse since now it's not working for all scaleTypes.

TWiStErRob commented 7 years ago

Any chance the ImageView is circular or otherwise custom? Try .dontAnimate() on all loads as well.

MMarco94 commented 7 years ago

Using .dontAnimate() doesn't have any effect on the issue: used in conjuncion with dontTransform it doesn't work well with all scaleTypes, used alone is doesn't work with the matrix scaleType.

TWiStErRob commented 7 years ago

Out of reasonable ideas... try .fitCenter() to force similar behavior to non-matrix. See GenericRequestBuilder.into(ImageView).

Can you please build a small repro app so I can try to debug (or you find the solution while building it ;)?

MMarco94 commented 7 years ago

Ok, I'll do it tomorrow. Thanks for your help in the meantime

TWiStErRob commented 7 years ago

I just noticed something, though I don't know how that can mess things up: the outer load is Image and the thumbs are all Strings/Uris. You could also try a getThumbnailLoader(context, image).into(imageView) to get this difference out of the picture and use uniform models.

MMarco94 commented 7 years ago

Calling getThumbnailLoader(context, image).into(imageView) displays the image with lower resolution, but that's intended, since the lower resolution image is the only one that is downloaded (I've clear the data of the app before installing) since the other downloaders are using NetworkDisablingLoader.

Anyway, is there a better way to achieve what I'm trying to do? I'm starting to think that the nested thumbnailing is really heavy: for example, in the worst case in which all the images are saved on disk, the app will load all of them (if I understand correctly how Glide works).

stale[bot] commented 6 years ago

This issue has been automatically marked as stale because it has not had activity in the last seven days. It will be closed if no further activity occurs within the next seven days. Thank you for your contributions.