使用Glide从Firebase存储中获取图像

时间:2017-10-09 17:59:05

标签: java android firebase firebase-storage android-glide

我正在尝试使用Glide从Firebase存储加载图像,但我收到错误。

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文件 -

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'

我收到错误:

error

3 个答案:

答案 0 :(得分:6)

似乎使用Firebase UI 3.0.0,Firebase支持Glide 4.0,并改变了使用Glide加载数据的方式。根据Github的文件:

要从StorageReference加载图像,请先在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());
    }
}

然后,您可以将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

如果您将Firabase UI降级到2.4.0,您的代码应该可以正常工作,但是在这种情况下,您很可能会收到支持库的混合版本错误。

答案 1 :(得分:2)

尝试这种方式:

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

因此,您可以获得存储中图片的网址,然后将该网址加载到滑行中

答案 2 :(得分:0)

(KOTLIN)在文档中没有提到任何地方,但是必须使用Glide将图像从Cloud Storage直接加载到您的应用中,您必须在app build.gradle中包含三行(除了其他Firebase依赖项):

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'

尽管建议您为Kotlin使用kotlin - kapt而不是annotationProcessor

然后在项目中的某些位置为Firebase Loader添加此类。请注意,注释对于该类非常重要:

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

然后您可以将其用作:

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

有任何错误吗?还要Invalidate Cache and Restart更新您的Android Studio中的依赖项。