【问题标题】:How to check if image exists in Firebase Storage?如何检查图像是否存在于 Firebase 存储中?
【发布时间】:2017-08-10 23:46:44
【问题描述】:

总而言之,我想在 Firebase 中为每个用户存储和检索个人资料图片。我正在尝试使用 FirebaseUI 和 Glide 从 Firebase 检索图像(如 here 所述)。

它非常适合我想要实现的目标,但我想处理用户没有上传自己的个人资料图片(当 Firebase 存储中没有图片时)的情况。遗憾的是,我无法找到如何检查 Firebase 存储中是否有这样的文件。有谁知道如何检查是否有文件profile.jpg

(在 Firebase 存储中)在以下代码中?

StorageReference storageRef =  storageReference.child("profile.jpg");

ImageView imageView = profilePicture;
// Load the image using Glide
Glide.with(context /* context */)
     .using(new FirebaseImageLoader())
     .load(storageRef)
     .into(imageView);

【问题讨论】:

  • 当您说“处理用户没有上传自己的头像的情况”时,您想调用具有业务逻辑的特定方法或仅在配置文件ImageView 中显示占位符/错误图标?
  • 我想为所有未上传自己的用户的个人资料图片提供默认图片。因此,“句柄”的意思是,如果 Firebase 存储中没有图片,我想显示默认的。

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


【解决方案1】:

如果 firebase 上没有 'profile.jpg' 则显示默认图片,在较新的 v4 Glide 中,您可以使用 RequestOptions,如下所示:

RequestOptions options = new RequestOptions().error(R.drawable.default_avatar);
Glide.with(context /* context */)
         .using(new FirebaseImageLoader())
         .load(storageRef)
         .apply(options)
         .into(imageView);

Glide v3 的原始答案

使用错误方法:

Glide.with(context /* context */)
     .using(new FirebaseImageLoader())
     .load(storageRef)
     .error(R.drawable.default_avatar)
     .into(imageView);

【讨论】:

  • 如果解决了您的问题,也请采纳。
  • 它对我不起作用。我还尝试了“.error(AppCompatResources.getDrawable(R.drawable.some_other_vector))”和其他一些,仍然无法正常工作=[
  • @MikeYan for Glide v4 需要不同的代码,我更新了答案
猜你喜欢
  • 2016-10-11
  • 2021-02-26
  • 2019-05-22
  • 1970-01-01
  • 1970-01-01
  • 2022-08-21
  • 1970-01-01
  • 2019-01-04
  • 2020-08-01
相关资源
最近更新 更多