Recover Firebase Storage Image without Downloading

Asked

Viewed 888 times

0

I made use of Firebase Storage to save images, now I want to grab them for users to see, there’s some way to do this without downloading the image?

2 answers

1

I use it as follows:

            refEst = storageRef.child('estabelecimentos').child(id + '.png');
            refEst.getDownloadURL().then(function (url) {
                return url;
            }).catch(function (error) {
                console.log("erro");
            });
  • Hello friend. I have several images in the folder so just grab one. How would you list all?

0

The taskSnapshot.getDownloadUrl() method has been removed in recent versions of the Firebase Storage SDK. You will need to get the download URL from Storagereference now.

Calling Storagereference.getDownloadUrl() returns a Task because you need to recover the server download URL. Therefore, you will need a completion listener to get the real URL.

I did it that way and it worked:

private void salvarFotoStorage(String urlString, final int sizeList, int counter){

    //Cria o nó storage
    StorageReference imagemAnuncio = storage.child("imagens").child("anuncios").child(anuncio.getIdAnuncio()).child("imagem"+contador);

    //Fazer upload do arquivo
    final UploadTask uploadTask = imagemAnuncio.putFile(Uri.parse(urlString));
    uploadTask.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
        @Override
        public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {

            taskSnapshot.getStorage().getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                        @Override
                        public void onSuccess(Uri uri) {
                            String urlConvertida = uri.toString();
                            Log.i("UPLOAD",  urlConvertida);


                            listaUrlFotos.add(urlConvertida);

                            if(tamanhoLista == listaUrlFotos.size()){
                                anuncio.setFotos(listaUrlFotos);
                                anuncio.salvar();

                                dialog.dismiss();
                                finish();
                            }
                        }
                    }).addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    Log.i("DOWNLOAD", "FALHA AO CAPTURAR DOWNLOAD"+e.getMessage());
                }
            });



        }
    }).addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {
            exibirMensagemErro("Falha ao fazer upload");
            Log.i("UPLOAD", "FALHA NO UPLOAD"+e.getMessage());
        }
    });

}

Browser other questions tagged

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