java - 在 Glide 中使用 Firebase 存储图像

标签 java android firebase android-glide firebaseui

有很多重复的答案我几乎都试过了,但我仍然无法将 Firebase 存储图像与 Glide 一起使用。

首先我使用的是docs

    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) at com.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'
}

最佳答案

改变这个:

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

为此:

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

根据 Glide 文档:

使用()

The using() API was removed in Glide 4 to encourage users to register their components once with a AppGlideModule to avoid object re-use. Rather than creating a new ModelLoader each time you load an image, you register it once in an AppGlideModule and let Glide inspect your model (the object you pass to load()) to figure out when to use your registered ModelLoader.

To make sure you only use your ModelLoader for certain models, implement handles() as shown above to inspect each model and return true only if your ModelLoader should be used.

using() 已从 Glide 4 中删除。

要解决这个问题,您需要这样做: 要从 StorageReference 加载图像,首先要注册一个 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

关于java - 在 Glide 中使用 Firebase 存储图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48762263/

相关文章:

java - 从 jFreeChart 毫秒到 java.util.Date

java - 如何在 linux 服务器上禁用 JDWP 接口(interface)?

javascript - 无法从 firebase 对象读取所有数据

firebase - 检测 Firebase 中的本地数据更新

ios - Google 社交互动事件未反射(reflect)在 Google Analytics 中

java - 在 Lucene 4.4.0 中搜索词干和精确词

java - Android 上的线程限制?

android - 调整图像大小以适合 LinearLayout

android - Facebook 与来自 Android 应用程序的元数据共享链接

android - 每次gradle同步后找不到Android SDK