从Firebase存储中获取图像

时间:2017-07-24 05:57:18

标签: android firebase firebase-storage

我是整个Firebase存储概念的新手。

我做了一点研究,发现了这个滑翔机。用于将图像添加到imageView。现在它给了我这个错误。

我该如何解决?

还有其他办法吗?

24 09:39:00.286 14919-14919/azcrew.eatapp E/AndroidRuntime: FATAL EXCEPTION: main
  Process: azcrew.eatapp, PID: 14919
  java.lang.IllegalArgumentException: Unknown type class com.google.firebase.storage.StorageReference. You must provide a Model of a type for which there is a registered ModelLoader, if you are using a custom model, you must first call Glide#register with a ModelLoaderFactory for your custom model class
    at com.bumptech.glide.RequestManager.loadGeneric(RequestManager.java:629)
    at com.bumptech.glide.RequestManager.load(RequestManager.java:598)
    at azcrew.eatapp.halfdone.Cari$2.onSuccess(Cari.java:76)
    at azcrew.eatapp.halfdone.Cari$2.onSuccess(Cari.java:73)
    at com.google.android.gms.tasks.zzj.run(Unknown Source)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:155)
    at android.app.ActivityThread.main(ActivityThread.java:5696)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)

这是代码

StorageReference mStorageRef = FirebaseStorage.getInstance().getReference();
final StorageReference imgRef = mStorageRef.child("images/img.jpg");
final long ONE_MEGABYTE = 1024*1024;


imgRef.getBytes(ONE_MEGABYTE).addOnSuccessListener(new OnSuccessListener<byte[]>() {
    @Override
    public void onSuccess(byte[] bytes) {
        Glide.with(Cari.this).load(imgRef).diskCacheStrategy(DiskCacheStrategy.ALL).into(kuryerImg);
    }
}).addOnFailureListener(new OnFailureListener() {
    @Override
    public void onFailure(@NonNull Exception e) {

    }
});

3 个答案:

答案 0 :(得分:3)

使用FirebaseImageLoader

Glide.with(this /* context */)
    .using(new FirebaseImageLoader())
    .load(storageReference)
    .into(imageView);

答案 1 :(得分:2)

我有另一种方法来解决问题。

您可以获取图片的下载网址。 这是Google指南中的代码。

StorageReference storageRef = 
  FirebaseStorage.getInstance().getReference();    
 storageRef.child("users/me/profile.png").getDownloadUrl()
.addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
    // Got the download URL for 'users/me/profile.png'
    })
.addOnFailureListener(new OnFailureListener() {
  @Override
  public void onFailure(@NonNull Exception exception) {
    // Handle any errors
   }
});

如果您需要向您的设备下载图像的指南,请参阅Stackoverflow链接: Download Image from URL

答案 2 :(得分:0)

使用Universal loader而不是使用滑行,它比滑翔更快地加载图像

   DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
        .cacheOnDisc(true).cacheInMemory(true)
        .imageScaleType(ImageScaleType.EXACTLY)
        .displayer(new FadeInBitmapDisplayer(300)).build();

ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
        getApplicationContext())
        .defaultDisplayImageOptions(defaultOptions)
        .memoryCache(new WeakMemoryCache())
        .discCacheSize(100 * 1024 * 1024).build();

ImageLoader.getInstance().init(config);

final ImageLoader imageLoader = ImageLoader.getInstance();

final DisplayImageOptions options = new DisplayImageOptions.Builder().cacheInMemory(true)
        .cacheOnDisc(true).resetViewBeforeLoading(true)
        .showImageForEmptyUri(R.mipmap.preview)
        .showImageOnFail(R.mipmap.preview)
        .build();


 mdatabase.child(Table_item).addValueEventListener(new ValueEventListener() {


    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        Map map = (Map) dataSnapshot.getValue();
        String image1 = (String) map.get("imagename");
        String image2 = (String) map.get("image2name");
        imageLoader.displayImage(ImageView1, image1, options, new ImageLoadingListener() {

            @Override
            public void onLoadingStarted(String imageUri, View view) {

            }

            @Override
            public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
                progressBar.setVisibility(View.GONE);
            }

            @Override
            public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {


            }

            @Override
            public void onLoadingCancelled(String imageUri, View view) {
                progressBar.setVisibility(View.GONE);
            }
        });
        imageLoader.displayImage(ImageView2, image2name, options, new ImageLoadingListener() {
            @Override
            public void onLoadingStarted(String imageUri, View view) {

            }

            @Override
            public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
                progressBar.setVisibility(View.GONE);
            }

            @Override
            public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                i++;
                progressstatus(i);
            }

            @Override
            public void onLoadingCancelled(String imageUri, View view) {
                progressBar.setVisibility(View.GONE);

            }
        });

使用以下依赖关系:

compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'