简体   繁体   中英

Getting Image from Firebase Storage using Glide

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 android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.firebase.ui.storage.images.FirebaseImageLoader;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.StorageReference;

public class Picture_act extends AppCompatActivity{
ImageView i1;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.picture_activity);
    FirebaseStorage storage = FirebaseStorage.getInstance();
    StorageReference storageReference = storage.getReference();
    i1 = (ImageView) findViewById(R.id.full_picture);
    Glide.with(this).using(new 
FirebaseImageLoader()).load(storageReference).into(i1);
}
}

Gradle File -

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
    exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.google.firebase:firebase-storage:11.4.2'
compile 'com.google.firebase:firebase-auth:11.4.2'
compile 'com.firebaseui:firebase-ui-storage:3.0.0'
compile 'com.android.support:appcompat-v7:25.1.0'
compile 'com.android.support:design:25.1.0'
compile 'com.github.devlight.navigationtabstrip:navigationtabstrip:1.0.4'

testCompile 'junit:junit:4.12'
compile 'com.github.bumptech.glide:glide:3.8.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.2.0'

}
apply plugin: 'com.google.gms.google-services'

I am getting a error:

错误

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

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.

( 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM