I have try to load https Image URL inside imagview using Glide 4.x. I have apply various developer answer inside my code but I didn't get success to load images. Finally I found a solution to load https image URL inside imageview using Glide. For that you have to follow below steps to attach certificate with glide request.
Step 1: You to import Latest glide library from glide official GitHub documentation. If you enable proguard then add code inside proguard file as per mention in glide documentation.
For Java developer
dependencies {
implementation 'com.github.bumptech.glide:glide:4.11.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0'
implementation("com.github.bumptech.glide:okhttp3-integration:4.11.0") {
exclude group: 'glide-parent'
}
}
For Kotling developer
dependencies {
implementation 'com.github.bumptech.glide:glide:4.11.0'
kapt 'com.github.bumptech.glide:compiler:4.11.0'
implementation("com.github.bumptech.glide:okhttp3-integration:4.11.0") {
exclude group: 'glide-parent'
}
}
Step 2: Once Glide Library successfully build with your project. UnsafeOkHttpClient clas. I have created this class in java but you can create this in kotlin as per your need.
public class OkHttpStreamFetcher implements DataFetcher<InputStream>, okhttp3.Callback {
private static final String TAG = "OkHttpFetcher";
private final Call.Factory client;
private final GlideUrl url;
private InputStream stream;
private ResponseBody responseBody;
private DataFetcher.DataCallback<? super InputStream> callback;
private volatile Call call;
// Public API.
@SuppressWarnings("WeakerAccess")
public OkHttpStreamFetcher(Call.Factory client, GlideUrl url) {
this.client = client;
this.url = url;
}
@Override
public void loadData(
@NonNull Priority priority, @NonNull final DataCallback<? super InputStream> callback) {
Request.Builder requestBuilder = new Request.Builder().url(url.toStringUrl());
for (Map.Entry<String, String> headerEntry : url.getHeaders().entrySet()) {
String key = headerEntry.getKey();
requestBuilder.addHeader(key, headerEntry.getValue());
}
Request request = requestBuilder.build();
this.callback = callback;
call = client.newCall(request);
call.enqueue(this);
}
@Override
public void onFailure(@NonNull Call call, @NonNull IOException e) {
if (Log.isLoggable(TAG, Log.DEBUG)) {
Log.d(TAG, "OkHttp failed to obtain result", e);
}
callback.onLoadFailed(e);
}
@Override
public void onResponse(@NonNull Call call, @NonNull Response response) {
responseBody = response.body();
if (response.isSuccessful()) {
long contentLength = Preconditions.checkNotNull(responseBody).contentLength();
stream = ContentLengthInputStream.obtain(responseBody.byteStream(), contentLength);
callback.onDataReady(stream);
} else {
callback.onLoadFailed(new HttpException(response.message(), response.code()));
}
}
@Override
public void cleanup() {
try {
if (stream != null) {
stream.close();
}
} catch (IOException e) {
// Ignored
}
if (responseBody != null) {
responseBody.close();
}
callback = null;
}
@Override
public void cancel() {
Call local = call;
if (local != null) {
local.cancel();
}
}
@NonNull
@Override
public Class<InputStream> getDataClass() {
return InputStream.class;
}
@NonNull
@Override
public DataSource getDataSource() {
return DataSource.REMOTE;
} }
Step 3: Now Start create on class which extends with AppGlideModule and @GlideModule annotation to that class.
After annotation Rebuild your project and it will create one class GlideApp class inside your project. This class help us to send SSL request when glide try to load https url.
For Java Developer
@GlideModule
public class MyAppGlideModule extends AppGlideModule {
@Override
public void registerComponents(@NonNull Context context, @NonNull Glide glide, @NonNull Registry registry) {
OkHttpClient okHttpClient= UnsafeOkHttpClient.getUnsafeOkHttpClient();
registry.replace(GlideUrl.class, InputStream.class, new OkHttpUrlLoader.Factory(okHttpClient));
}
}
For Kotlin Devloper
@GlideModule
class AppGlideModule : AppGlideModule(){
override fun registerComponents(@NonNull context: Context, @NonNull glide: Glide, @NonNull registry: Registry) {
val okHttpClient = UnsafeOkHttpClient.getUnsafeOkHttpClient()
registry.replace(GlideUrl::class.java, InputStream::class.java, OkHttpUrlLoader.Factory(okHttpClient))
}
}
Step 4: Use the GlideApp such as GlideApp.with(this).load(imgUrl).into(glide_test_iv1)
Summary:
Glide 4.0 need not have declare "GlideModule" in AndroidManifest.xml. You just need to following steps:
YourAppGlideModule extends AppGlideModule, you can override function applyOptions in the YourAppGlideModule class.
You should make project in "android studio -> build -> make project", it will generate the GlideApp class.
Use the GlideApp such as GlideApp.with(this).load(imgUrl).into(glide_test_iv1)