Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use GlideModule on Glide 4?

I recently update my app to use Glide 4, to be precise, Glide 4.2.0. gradle:

compile 'com.github.bumptech.glide:glide:4.2.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.2.0'
compile ('com.github.bumptech.glide:okhttp3-integration:4.2.0'){
    exclude group: 'glide-parent'
}

in manifest:

<meta-data
            android:name="com.xxx.MyGlideModule"
            android:value="GlideModule"/>

GlideModule class:

@GlideModule
public class MyGlideModule extends AppGlideModule {

    @Override
    public void registerComponents(Context context, Glide glide, Registry registry) {
        OkHttpClient client = new OkHttpClient.Builder()
                .readTimeout(30, TimeUnit.SECONDS)
                .writeTimeout(30, TimeUnit.SECONDS)
                .connectTimeout(30, TimeUnit.SECONDS)
                .build();

        OkHttpUrlLoader.Factory factory = new OkHttpUrlLoader.Factory(client);

        glide.getRegistry().replace(GlideUrl.class, InputStream.class, factory);
    }
}

how I use glide inside an adapter:

        RequestOptions myOptions = new RequestOptions()
                .placeholder(R.drawable.ic_placeholder)
                .diskCacheStrategy(DiskCacheStrategy.NONE)
                .dontAnimate()
                .skipMemoryCache(true)
                ;

        Glide.with(mContext)
                .load(Imageid[position])
                .apply(myOptions)
                .into(imageView);

with these code, when I run it I got error:

java.lang.RuntimeException: Expected instanceof GlideModule, but found: [my app package].MyGlideModule@d1c2328
  at com.bumptech.glide.module.ManifestParser.parseModule(ManifestParser.java:81)
  at com.bumptech.glide.module.ManifestParser.parse(ManifestParser.java:43)
  at com.bumptech.glide.Glide.initializeGlide(Glide.java:193)
  at com.bumptech.glide.Glide.checkAndInitializeGlide(Glide.java:172)
  at com.bumptech.glide.Glide.get(Glide.java:156)
  at com.bumptech.glide.Glide.getRetriever(Glide.java:540)
  at com.bumptech.glide.Glide.with(Glide.java:566)
  at [adapter line where I implement Glide]

how can I use MyGlideModule ?

like image 727
Dika Avatar asked Oct 09 '17 02:10

Dika


2 Answers

Glide 4.0 need not have declare "GlideModule" in AndroidManifest.xml. You just need to following steps:

  1. YourGlideModule extends AppGlideModule, you can override function applyOptions in the YourGlideModule class.
  2. You should make project in "android studio -> build -> make project", it will generate the GlideApp class.
  3. Use the GlideApp such as GlideApp.with(this).load(imgUrl).into(glide_test_iv1)
like image 138
kevin Avatar answered Oct 31 '22 12:10

kevin


If GlideApp can't be generated then -

Make sure you have build.gradle dependency of annotationProcessor 'com.github.bumptech.glide:compiler:4.9.0'

Make sure your UnsafeOkHttpGlideModule extends AppGlideModule and also in meta-data the android:value="" should be android:value="AppGlideModule"

like image 3
brady lee Avatar answered Oct 31 '22 12:10

brady lee