How do I know if a file exists in Storage Firabase?

Asked

Viewed 130 times

0

I’m using Firebase Storage to store a user profile photo, I made a method to search the image and place it in an Imageview. It works very well when the image exists in Firebase, but when it doesn’t exist, it leaves Imageview with nothing, it doesn’t even show the default image I put in XML. How do I treat when the method does not return an image?

private void buscarFotoPerfil(String idUsuario) {

    try {
        storage = ConexaoFirebase.getStorageReference()
                .child("images/perfil/" + idUsuario + ".jpg");

        Glide.with(this)
                .using(new FirebaseImageLoader())
                .load(storage)
                .into(imagePerfil);

    } catch (Exception e) {
        Toast.makeText(this, "Erro ao carregar foto do perfil." + e, Toast.LENGTH_SHORT).show();
    }
}

1 answer

0


You can use the method placeholder() while loading the image and method error() if an error occurs in the image loading. See:

Glide 4.x:

GlideApp.with(this)
    .using(new FirebaseImageLoader())
    .load(storage)
    .placeholder(R.drawable.image_loading)
    .error(R.drawable.image_error)
    .into(imagePerfil);

Glide 3.x:

Glide.with(this)
    .using(new FirebaseImageLoader())
    .load(storage)
    .placeholder(R.drawable.image_loading)
    .error(R.drawable.image_error)
    .into(imagePerfil);
  • 1

    Solved my problem 100%, thanks anyway!

  • @Henriqueedu if the answer helped solve your problem, if you want you can validate it. If there are any more questions, I’m here.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.