how to use GlideModule on Glide 4?

末鹿安然 提交于 2019-12-05 15:13:02

问题


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 ?


回答1:


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)



回答2:


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"



来源:https://stackoverflow.com/questions/46638056/how-to-use-glidemodule-on-glide-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!