Glide 不从 StorageReference 加载图像

Glide not Loading image from StorageReference

我在使用 Glide 4.9.0(最新)将图像从 StorageReference 加载到 ImageView 时遇到问题。

它卡在了 PlaceHolder 上.. 当我尝试放置可下载的 url 我的 ref 时,它正在工作..

StorageReference ref = FirebaseStorage.getInstance().getReference().child("myImage")

//It's not working
Glide.with(mContext)
   .load(ref)
   .placeholder(R.drawable.ic_loading_circle)
   .error(R.drawable.ic_loading_circle)
   .into(mImageView);

//It's working
ref.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>()
{
    @Override
    public void onSuccess(Uri downloadUrl)
    {
        Glide.with(mContext)
            .load(downloadUrl.toString())
            .placeholder(R.drawable.ic_loading_circle)
            .error(R.drawable.ic_loading_circle)
            .into(mImageView);
    }
});

我在使用 try-catch 时没有看到任何错误,我已经在 Manifest 中设置了 Internet 权限。来自 drawable 的其他图像也可以正常工作

注意:我的 firebase 被 2 个应用程序使用,第一个应用程序使用 Glide 没有任何问题,但是第二个应用程序遇到了未知问题。

任何解决方案或替代方案?因为使用 getDownloadUrl().addOnSuccessListener 获取 URI 太慢

适合您的解决方案是正确的解决方案。我也是这样做的。

profileListener = new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            String imageUrl = dataSnapshot.getValue(String.class);

            if (ProfileFragment.this.getActivity() != null)
                Glide.with(ProfileFragment.this.getActivity())
                        .load(imageUrl)
                        .placeholder(R.drawable.default_image)
                        .circleCrop()
                        .into(photoPhoto);
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    };

如果您发现这个东西很慢,只需在 firebase 中启用离线持久性。

FirebaseDatabase.getInstance().setPersistenceEnabled(true);

在打开应用程序时调用任何 FirebaseDatabase 实例之前。

Firebase 非常有效地管理同步。

注意:- 每当您在开发应用程序时发现任何随机异常调用快照,请尝试清除应用程序的数据或重新安装。

对我来说效果很好!

我不认为 Glide 支持 StorageReference 这是不同的协议,但你可以使用支持这个的 FirebaseUI,并在幕后使用 Glide 库。

FirebaseUI

Using FirebaseUI you can quickly and easily download, cache, and display images from Cloud Storage using our integration with Glide.

First, add FirebaseUI to your app/build.gradle:

dependencies { // FirebaseUI Storage only implementation 'com.firebaseui:firebase-ui-storage:4.3.1' }

然后您可以将图像直接从 Storage 加载到 ImageView 中:

// Reference to an image file in Cloud Storage
StorageReference storageReference = FirebaseStorage.getInstance().getReference();

// ImageView in your Activity
ImageView imageView = findViewById(R.id.imageView);

// Download directly from StorageReference using Glide
// (See MyAppGlideModule for Loader registration)
GlideApp.with(this /* context */)
        .load(storageReference)
        .into(imageView);

Firebase Documentation for loading image directly via Glide

上面的答案对我不起作用。

在 Glide 4.8.0 中,这对我有用:

  1. 首先确保您的应用程序中包含以下内容build.gradle

    implementation 'com.firebaseui:firebase-ui-storage:3.2.2'

    implementation 'com.github.bumptech.glide:glide:4.8.0'

    annotationProcessor 'com.github.bumptech.glide:compiler:4.8.0'

2.Then 使用以下代码从存储引用中加载图像

    DrawableCrossFadeFactory factory =
            new DrawableCrossFadeFactory.Builder().setCrossFadeEnabled(true).build();

    RequestOptions options =
            new RequestOptions()
                    .centerCrop()
                    .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
                    .placeholder(R.drawable.your_progress_animation)
                    .error(R.drawable.ic_your_on_error_image);

    // Download directly from StorageReference using Glide
    // (See MyAppGlideModule for Loader registration)
    Glide.with(getAppContext())
            .load(item.reference)
            .transition(withCrossFade(factory))
            .apply(options)
            .into(holder.signatureImageView);