Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

glide image loading timeout increase

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?

like image 341
Karthik K M Avatar asked Mar 31 '16 16:03

Karthik K M


People also ask

Why does glide keep reloading the same images?

Maybe there's a bug in it: if it doesn't recycle the views, then Glide will keep loading the old images. This means the any scrolled images will be queued up after the old ones complete.

How to force glide to load a lower resolution image?

If you only have a single remote URL, you can still benefit from the thumbnail API by using Glide’s override () or sizeMultiplier () APIs to force Glide to load a lower resolution image in the thumbnail request In case when image url is changed and old one is never used but it is still in cache and eating up the phone memory, what to do?

What is Glide in Android?

Glide is an Image Loader Library for Android developed by bumptech and is a library that is recommended by Google. It has been used in many Google open source projects including Google I/O 2014 official application. Many of us use glide for image loading but only some of us know its real power.

What can I do with image and GIFs in OpenGlide?

Glide supports fetching, decoding, and displaying video stills, images, and animated GIFs. Glide includes a flexible API that allows developers to plug in to almost any network stack.


3 Answers

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" />
like image 56
Karthik K M Avatar answered Sep 23 '22 16:09

Karthik K M


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()));
    }

}
like image 25
Jing Li Avatar answered Sep 21 '22 16:09

Jing Li


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()));
    }
}
like image 39
chubao Avatar answered Sep 21 '22 16:09

chubao