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?
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.
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?
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.
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.
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" />
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()));
}
}
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()));
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With