Getting Image from Firebase Storage using Glide

前端 未结 3 1713
渐次进展
渐次进展 2020-12-06 06:47

I am trying to load an image from Firebase Storage using Glide but I am getting an error .

package com.kanishq.wallpaper;
import android.os.Bundle;
import an         


        
相关标签:
3条回答
  • 2020-12-06 07:20

    Try this way:

    storageReference.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                        @Override
                        public void onSuccess(Uri uri) {
                            imageURL = uri.toString();
                            Glide.with(getApplicationContext()).load(imageURL).into(i1);
                        }
                    }).addOnFailureListener(new OnFailureListener() {
                        @Override
                        public void onFailure(@NonNull Exception exception) {
                            // Handle any errors
                        }
                    });
    

    So this way, you get a URL to the image in the storage and you load that URL into the glide

    0 讨论(0)
  • 2020-12-06 07:21

    It seems that with Firebase UI 3.0.0, Firebase has Glide 4.0 support and has changed the way the data is loaded using Glide. According to documentation at Github:

    To load an image from a StorageReference, first register in your AppGlideModule:

    @GlideModule
    public class MyAppGlideModule extends AppGlideModule {
        @Override
        public void registerComponents(Context context, Registry registry) {
            // Register FirebaseImageLoader to handle StorageReference
            registry.append(StorageReference.class, InputStream.class,
                    new FirebaseImageLoader.Factory());
        }
    }
    

    Then you can 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);
    

    (Source: https://github.com/firebase/FirebaseUI-Android/tree/master/storage)

    If you downgrade Firebase UI to 2.4.0, your code should work, however in that case you will most probably receive mixing version errors with support libraries.

    0 讨论(0)
  • 2020-12-06 07:24

    ( KOTLIN ) It was not mentioned anywhere in the Docs but to have to load images directly from Cloud Storage to your App using Glide you have to include three lines in app build.gradle (in addition to other Firebase dependencies):

    implementation 'com.github.bumptech.glide:glide:4.11.0'
    annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0'
    implementation 'com.firebaseui:firebase-ui-storage:6.2.0'
    

    Though you are adviced to use kotlin - kapt instead of annotationProcessor for Kotlin.

    Then some where in your Project add this class for Firebase Loader. Note that the annotation is very important for the class:

    package com.your.package.name
    
    import android.content.Context
    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
    class MyAppGlideModule : AppGlideModule() {
        override fun registerComponents(context: Context, glide: Glide, registry: Registry) {
            registry.append(
                StorageReference::class.java, InputStream::class.java,
                FirebaseImageLoader.Factory()
            )
        }
    }
    

    Then you can use it as:

    Glide.with(this /* context */)
        .load(storageReference)
        .into(imageView)
    

    Any errors? Update the dependencies above also Invalidate Cache and Restart your Android Studio.

    0 讨论(0)
提交回复
热议问题