3

Am using glide v4 and okhttp3 integration with glide. I want to change it's timeout time. how to do that? by extending AppGlideModule or is there any other way? I have searched for proper documentation but not seen anywhere.

Zoe
  • 27,060
  • 21
  • 118
  • 148
Bincy Baby
  • 3,941
  • 5
  • 36
  • 62

1 Answers1

5

You can change the timeout by creating a class, which extends AppGlideModule and has annotation @GlideModule. Then you override the method registerComponents and inside it, you can create a new OkHttpClient, which you can use to control the Glide request timeout.

First, you should add Glide and OkHttp3 Glide Integration library gradle dependencies in the build.gradle file:

compile 'com.github.bumptech.glide:glide:4.2.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.2.0'
compile("com.github.bumptech.glide:okhttp3-integration:4.2.0") {
    exclude group: 'glide-parent'
}

Second, the sample code for the custom GlideAppModule class:

@GlideModule
public class MyGlideAppModule extends AppGlideModule {
    @Override
    public void registerComponents(Context context, Glide glide, Registry registry) {
        OkHttpClient client = new OkHttpClient.Builder()
                .connectTimeout(20, TimeUnit.SECONDS)
                .readTimeout(20, TimeUnit.SECONDS)
                .build();

        OkHttpUrlLoader.Factory factory = new OkHttpUrlLoader.Factory(client);
        glide.getRegistry().replace(GlideUrl.class, InputStream.class, factory);
    }
}

Credits to Amir Ziarati for providing the solution!

Also, refer to this article about Glide customization with modules for more information about the differences between Glide 3.x and Glide 4.x.

Slavi Petrov
  • 320
  • 6
  • 12