我正试图使用FirebaseUI从Firebase Storage检索照片,并使用Glide直接在我的图像视图中显示。为了做到这一点,我创建了一个ModelLoader,如Firebase文档:中所示
import com.bumptech.glide.Glide;
import com.bumptech.glide.Registry;
import com.bumptech.glide.annotation.GlideModule;
import com.bumptech.glide.module.AppGlideModule;
import com.firebase.ui.storage.images.FirebaseImageLoader;
import com.google.firebase.storage.StorageReference;
import java.io.InputStream;
@GlideModule
public class MyAppGlideModule extends AppGlideModule {
@Override
public void registerComponents(Context context, Glide glide, Registry registry) {
// Register FirebaseImageLoader to handle StorageReference
registry.append(StorageReference.class, InputStream.class,
new FirebaseImageLoader.Factory());
}
}
这个文件在我的项目文件夹中,所以gradle应该能识别它。问题是,每次我运行应用程序并执行这行代码时:Glide.with(context!!).load(reference).into(binding.imgPhoto)
出现此错误:
W/Glide: Failed to find GeneratedAppGlideModule. You should include an annotationProcessor compile dependency on com.github.bumptech.glide:compiler in your application and a @GlideModule annotated AppGlideModule implementation or LibraryGlideModules will be silently ignored
E/GlideExecutor: Request threw uncaught throwable
com.bumptech.glide.Registry$NoModelLoaderAvailableException: Failed to find any ModelLoaders registered for model class: class com.google.firebase.storage.StorageReference
at com.bumptech.glide.load.model.ModelLoaderRegistry.getModelLoaders(ModelLoaderRegistry.java:77)
at com.bumptech.glide.Registry.getModelLoaders(Registry.java:585)
at com.bumptech.glide.load.engine.DecodeHelper.getLoadData(DecodeHelper.java:207)
at com.bumptech.glide.load.engine.DecodeHelper.getCacheKeys(DecodeHelper.java:224)
at com.bumptech.glide.load.engine.ResourceCacheGenerator.startNext(ResourceCacheGenerator.java:44)
at com.bumptech.glide.load.engine.DecodeJob.runGenerators(DecodeJob.java:310)
at com.bumptech.glide.load.engine.DecodeJob.runWrapped(DecodeJob.java:276)
at com.bumptech.glide.load.engine.DecodeJob.run(DecodeJob.java:234)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:919)
at com.bumptech.glide.load.engine.executor.GlideExecutor$DefaultThreadFactory$1.run(GlideExecutor.java:393)
发生了什么?我已经创建了Loader、Invalidated Caches、Restarted,但问题仍然没有解决。。。
您需要创建一个扩展AppGlideModule的类(例如MyAppGlideModule(,并使用GlideApp.with…而不是Glide.with…查看此链接以获得完整的解释
类MyAppGlideModule可以位于源目录中的任何位置,并在编译时由Glide注释处理器进行处理,以创建GlideApp类。
对于kotlin:
@GlideModule
class AppGlide: AppGlideModule(){
override fun registerComponents(
context: android.content.Context,
glide: Glide,
registry: Registry
) {
super.registerComponents(context, glide, registry)
registry.append(
StorageReference::class.java, InputStream::class.java,
FirebaseImageLoader.Factory()
)
}
}