如何检查 Firebase 存储中是否存在图像?

How to check if image exists in Firebase Storage?

总而言之,我想在 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);

如果 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);