Using RequestOptions in AppGlideModule with Glide 4

六眼飞鱼酱① 提交于 2019-12-08 19:49:49

问题


I used the `Glide library with AppGlideModule, version of library 4.1.1. Here is my glide module class:

@GlideModule
public class GlideUtil extends AppGlideModule {

    private final int IMAGE_CACHE_SIZE = 20 * 1024 * 1024; // 20 MB
    private final String IMAGE_FOLDER = "/User/Images";

    @Override
    public void applyOptions(Context context, GlideBuilder builder) {
        RequestOptions requestOptions = new RequestOptions();
        requestOptions.format(DecodeFormat.PREFER_ARGB_8888);
        requestOptions.diskCacheStrategy(DiskCacheStrategy.ALL);
        builder.setDefaultRequestOptions(requestOptions);
        InternalCacheDiskCacheFactory factory = new InternalCacheDiskCacheFactory(context, IMAGE_FOLDER, IMAGE_CACHE_SIZE);
        builder.setDiskCache(factory);

    }

    @Override
    public boolean isManifestParsingEnabled() {
        return false;
    }

This code worked successfully. But when i updated version of glide library to 4.3.1

compile 'com.github.bumptech.glide:glide:4.3.1' 
annotationProcessor 'com.github.bumptech.glide:compiler:4.3.1'

in GlideUtil class i saw messages: "The result of format is not used", "The result of diskCacheStrategyis not used":

So, how to resolve this? And do the methods diskCacheStrategy and format work on Glide 4.3.1 ?


回答1:


The problem is, that you are not using the builder object, that is returned by format(), thus your actions become pointless, that's why lint warns you. You can see that method annotated with @CheckResult, that's how lint understands, that you are on a wrong way, because you are "not checking the result" returned by that method.

Instead perform following:


    RequestOptions requestOptions = new RequestOptions();
    requestOptions = requestOptions.format(DecodeFormat.PREFER_ARGB_8888);
    requestOptions = requestOptions.diskCacheStrategy(DiskCacheStrategy.ALL);

Now the warning would be gone.

Or you may perform following directly:


    builder.setDefaultRequestOptions(new RequestOptions()
                                        .format(DecodeFormat.PREFER_ARGB_8888)
                                        .diskCacheStrategy(DiskCacheStrategy.ALL));



来源:https://stackoverflow.com/questions/47265517/using-requestoptions-in-appglidemodule-with-glide-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!