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