将 Firebase 存储映像与 Glide 配合使用

我几乎尝试了大量重复的答案,但我仍然无法将Firebase存储映像与Glide一起使用。

首先,我正在使用文档

    FirebaseStorage storage  = FirebaseStorage.getInstance();
    StorageReference storageRef = storage.getReference();
    StorageReference pathReference = storageRef.child("sorular/1.jpg");

   // ImageView in your Activity
   ImageView imageView = rootView.findViewById(R.id.imageView);

   // Load the image using Glide
   Glide.with(this /* context */)
        .using(new FirebaseImageLoader()) // Cannot resolve method 'using
        .load(pathReference)
        .into(imageView);

如果我清理了 Glide 的 .using 部分,logcat 它会给出这个错误:

E/GlideExecutor: Request threw uncaught throwable com.bumptech.glide.Registry$NoModelLoaderAvailableException: Failed to find any ModelLoaders for model: gs://123...appspot.com/sorular/1.jpg
at com.bumptech.glide.Registry.getModelLoaders(Registry.java:227)
at com.bumptech.glide.load.engine.DecodeHelper.getLoadData(DecodeHelper.java:179) at com.bumptech.glide.load.engine.DecodeHelper.getCacheKeys(DecodeHelper.java:197) atcom.bumptech.glide.load.engine.ResourceCacheGenerator.startNext(ResourceCacheGenerator.java:41) at com.bumptech.glide.load.engine.DecodeJob.runGenerators(DecodeJob.java:282) at com.bumptech.glide.load.engine.DecodeJob.runWrapped(DecodeJob.java:249) at com.bumptech.glide.load.engine.DecodeJob.run(DecodeJob.java:222)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607) at java.lang.Thread.run(Thread.java:761)
at com.bumptech.glide.load.engine.executor.GlideExecutor$DefaultThreadFactory$1.run(GlideExecutor.java:347)

那么,如何以最佳方式在我的Android应用程序中使用firebase存储图像呢?

这也是我的构建 gradle 依赖项:

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:27.0.2'
    implementation 'com.android.support:palette-v7:27.0.2'
    implementation "com.android.support:cardview-v7:27.0.2"
    implementation "com.android.support:recyclerview-v7:27.0.2"
    implementation "com.android.support:support-v4:27.0.2"
    implementation 'com.android.support:design:27.0.2'

    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    implementation 'com.github.florent37:materialviewpager:1.2.3'


    implementation 'com.google.firebase:firebase-database:11.8.0'
    implementation 'com.google.firebase:firebase-storage:11.8.0'
    implementation 'com.firebaseui:firebase-ui-storage:2.0.1'
    implementation 'com.google.firebase:firebase-auth:11.8.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}

答案 1

更改此设置:

 implementation 'com.firebaseui:firebase-ui-storage:2.0.1'

对此:

  implementation 'com.firebaseui:firebase-ui-storage:3.2.1'

根据Glide文档:

using()

在 Glide 4 中删除了 using() API,以鼓励用户向 AppGlideModule 注册一次组件,以避免对象重用。不是每次加载图像时都创建新的 ModelLoader,而是在 AppGlideModule 中注册一次,然后让 Glide 检查您的模型(您传递给 load() 的对象)以确定何时使用已注册的 ModelLoader。

为了确保您只对某些模型使用 ModelLoader,请实现 handles() 如上所示来检查每个模型,并且仅当应使用 ModelLoader 时才返回 true。

using()已从 Glide 4 中删除。

要解决此问题,您需要执行以下操作:要从存储引用加载图像,请首先注册一个 AppGlideModule:

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

创建 AppGlideModule 类并完成干净构建后,可以使用 GlideApp 将 StorageReference 加载到 ImageView 中:

// Reference to an image file in Cloud Storage
StorageReference storageReference = ...;

// ImageView in your Activity
ImageView imageView = ...;

// Download directly from StorageReference using Glide
// (See MyAppGlideModule for Loader registration)
GlideApp.with(this /* context */)
        .load(storageReference)
        .into(imageView);

更多信息在这里: https://github.com/firebase/FirebaseUI-Android/tree/master/storage


答案 2

我知道我有点晚了,但它可能会帮助你们中的一些人。在 中使用这两种方法。app build.gradle

implementation 'com.github.bumptech.glide:glide:4.10.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.10.0' //For Kotlin You should use kapt instead of annotationProcessor though.

然后添加此类:

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

 GlideApp.with(getActivity()).load(storageReference).into(profileImg);

最后,您需要转到文件>使缓存无效并重新启动完成:)


推荐