19

I am using glide to load images from URL. While I am fetching the images I am showing a loader in the image view. Some of the images being fetched are larger and therefore in slow internet connection timeout occurs and throws exception

How can I increase the timeout?

Sam Judd
  • 7,317
  • 1
  • 38
  • 38
Karthik K M
  • 619
  • 2
  • 9
  • 24
  • Did you try using OK HTTP instead of volley? Like explained here: https://github.com/bumptech/glide/wiki/Integration-Libraries#okhttp – alexislg May 30 '16 at 16:12

5 Answers5

14

You can use this method of the new version of glide

.timeout(60000)

the final code sample will be:

Glide.with(imageView.getContext())
        .load(finalUrl)
        .timeout(60000)
        .placeholder(R.drawable.place_holder)
        .into(imageView);
Mahdi Zareei
  • 1,299
  • 11
  • 18
11

Below is the solution for: Glide 4.3.1 & OkHttp 3.9.1, a bit different than before (it's no more OkHttpGlideModule but AppGlideModule).

build.gradle

implementation 'com.squareup.okhttp3:okhttp:3.9.1'
implementation 'com.github.bumptech.glide:glide:4.3.1'
annotationProcessor 'com.github.bumptech.glide:compiler:4.3.1'
implementation 'com.github.bumptech.glide:okhttp3-integration:4.3.1'

CustomGlideModule

@GlideModule
public class CustomGlideModule extends AppGlideModule {

    @Override
    public void registerComponents(Context context, Glide glide, Registry registry) {
        final OkHttpClient.Builder builder = new OkHttpClient.Builder();
        builder.readTimeout(YOUR_CUSTOM_TIMEOUT, TimeUnit.SECONDS);
        builder.writeTimeout(YOUR_CUSTOM_TIMEOUT, TimeUnit.SECONDS);
        builder.connectTimeout(YOUR_CUSTOM_TIMEOUT, TimeUnit.SECONDS);
        registry.append(GlideUrl.class, InputStream.class, new OkHttpUrlLoader.Factory(builder.build()));
    }

}
Jing Li
  • 14,547
  • 7
  • 57
  • 69
10

After searching a lot finally got an answer, if you are using volley:

public class CustomGlide implements GlideModule {
    @Override
    public void applyOptions(Context context, GlideBuilder builder) {
    }

    @Override
    public void registerComponents(Context context, Glide glide) {
        RequestQueue queue = new RequestQueue( // params hardcoded from Volley.newRequestQueue()
                new DiskBasedCache(new File(context.getCacheDir(), "volley")),
                new BasicNetwork(new HurlStack())) {
            @Override public <T> Request<T> add(Request<T> request) {
                request.setRetryPolicy(new DefaultRetryPolicy(10000, 1, 1));
                return super.add(request);
            }
        };
        queue.start();
        glide.register(GlideUrl.class, InputStream.class, new VolleyUrlLoader.Factory(queue));
    }
}

Change the DefaultRetryPolicy according to your need

And in the manifest:

 <meta-data
            android:name="<package-name>.CustomGlide"
            android:value="GlideModule" />
Karthik K M
  • 619
  • 2
  • 9
  • 24
  • I have declared customGlide class as you say but now how to use it in glide load image line in java class ? – Dharmishtha May 23 '17 at 06:18
  • 1
    Now the glide is configured with the timeout you provided. Just request the image using Glide.with(context).load(url).into(ImageView) – Karthik K M May 23 '17 at 07:05
  • 2
    For VolleyUrlLoader you need to add: compile 'com.github.bumptech.glide:volley-integration:1.5.0@aar' to your dependencies. – The Dude Jun 19 '17 at 13:04
  • To avoid [conflict](https://github.com/bumptech/glide/wiki/Configuration#conflicting-glidemodules) with `VolleyGlideModule` that is added automatically to merged manifest if you use Gradle, you must manually exclude it by adding another metadata entry: `` – knezmilos Aug 17 '17 at 13:07
  • @knezmilos it shows me unresolved supertype error at compilation on VolleyUrlLoader – Zicsus Sep 19 '18 at 15:44
10

If you would like to use OkHttp, please import glide:okhttp-integration according to this, and then implement your own OkHttpGlideModule:

@GlideModule
public class CustomGlideModule extends OkHttpGlideModule {
    @Override
    public void applyOptions(Context context, GlideBuilder builder) {
        // stub
    }

    @Override
    public void registerComponents(Context context, Glide glide) {
        final OkHttpClient.Builder builder = new OkHttpClient.Builder();

        // set your timeout here
        builder.readTimeout(30, TimeUnit.SECONDS);
        builder.writeTimeout(30, TimeUnit.SECONDS);
        builder.connectTimeout(30, TimeUnit.SECONDS);

        glide.register(GlideUrl.class, InputStream.class, new OkHttpUrlLoader.Factory(builder.build()));
    }
}
Howard
  • 53
  • 2
  • 9
chubao
  • 5,871
  • 6
  • 39
  • 64
5
.timeout(30000)

Shortest way to do that:

Glide.with(this)
         .setDefaultRequestOptions(new RequestOptions()
         .timeout(30000))
         .load("https://stackoverflow.com/posts/58063088/edit")
         .placeholder(getResources().getDrawable(R.drawable.no_image))
         .error(getResources().getDrawable(R.drawable.no_image))                
         .into(imageView);
Muahmmad Tayyib
  • 689
  • 11
  • 28