将 Firebase 存储图像与 Glide 结合使用 [英] Using Firebase Storage image with Glide

查看:32
本文介绍了将 Firebase 存储图像与 Glide 结合使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有大量重复的答案,我几乎尝试过所有答案,但我仍然无法在 Glide 中使用 Firebase 存储图像.

There are tons of duplicated answers I had tried almost all of them but I am still not able to use Firebase storage image with 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 会出现此错误:

if I clean the .using part of Glide, logcat it gives this error:

E/GlideExecutor: Request throw uncaught throwable com.bumptech.glide.Registry$NoModelLoaderAvailableException: Failed to找到模型的任何模型加载器:gs://123...appspot.com/sorular/1.jpg
在 com.bumptech.glide.Registry.getModelLoaders(Registry.java:227)
在com.bumptech.glide.load.engine.DecodeHelper.getLoadData(DecodeHelper.java:179)在com.bumptech.glide.load.engine.DecodeHelper.getCacheKeys(DecodeHelper.java:197)在com.bumptech.glide.load.engine.ResourceCacheGenerator.startNext(ResourceCacheGenerator.java:41)在com.bumptech.glide.load.engine.DecodeJob.runGenerators(DecodeJob.java:282)在com.bumptech.glide.load.engine.DecodeJob.runWrapped(DecodeJob.java:249)在 com.bumptech.glide.load.engine.DecodeJob.run(DecodeJob.java:222)
在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)在java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)在 java.lang.Thread.run(Thread.java:761)
在com.bumptech.glide.load.engine.executor.GlideExecutor$DefaultThreadFactory$1.run(GlideExecutor.java:347)

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 存储图像?

So how can use firebase storage images in my android app in a best way?

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

also this my build gradle dependencies:

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 文档:

According to the Glide docs:

using()

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

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.

为确保您仅将 ModelLoader 用于某些模型,请实现如上所示的 handles() 以检查每个模型并仅在应使用您的 ModelLoader 时返回 true.

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 中删除.

using() was removed from Glide 4.

要解决这个问题,您需要这样做:要从 StorageReference 加载图像,首先注册一个 AppGlideModule:

To Solve this, you need to do this: To load an image from a StorageReference, first register an 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 中:

Once you have created an AppGlideModule class and done a clean build, you can use GlideApp to load a StorageReference into an 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-安卓/树/主/存储

这篇关于将 Firebase 存储图像与 Glide 结合使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆