I can’t return the url of an android image

Asked

Viewed 41 times

-1

I’m using the following code to record data and images in firebase and it’s even working, but the image link I’m not able to capture.

var uploadTesk: UploadTask = imageRef.putBytes(dadosImage!!);


        uploadTesk.addOnFailureListener(object: OnFailureListener{
            override fun onFailure(p0: Exception) {
                Toast.makeText(activity, "Error ao enviar imagem", Toast.LENGTH_LONG).show();
            }

        }).addOnSuccessListener(object:OnSuccessListener<UploadTask.TaskSnapshot>{
            override fun onSuccess(p0: UploadTask.TaskSnapshot?) {

                imageRef.downloadUrl.addOnSuccessListener(object:OnSuccessListener<Uri> {
                    override fun onSuccess(p0: Uri?) {
                        **newItemMenu.photoItem = p0.toString()**;
                    }

                }).addOnFailureListener(object: OnFailureListener{
                    override fun onFailure(p0: Exception) {
                        Toast.makeText(activity, p0.message, Toast.LENGTH_LONG).show();
                    }

                })

                //Save menu
                if ( newItemMenu.save() ){
                    Toast.makeText(activity, "Sucesso ao cadastrar produto!", Toast.LENGTH_LONG).show();
                }
            }

        })

Where has newItemMenu.photoItem = P0.toString(); it does not add the url in my class to be saved, I already used the log and Toast it shows the right url but it is not passing the value to save class in the database.. Could you help me?

1 answer

0


Puts the save inside the TASK to get the URL

        var uploadTesk: UploadTask = imageRef.putBytes(dadosImage!!);


    uploadTesk.addOnFailureListener(object: OnFailureListener{
        override fun onFailure(p0: Exception) {
            Toast.makeText(activity, "Error ao enviar imagem", Toast.LENGTH_LONG).show();
        }

    }).addOnSuccessListener(object:OnSuccessListener<UploadTask.TaskSnapshot>{
        override fun onSuccess(p0: UploadTask.TaskSnapshot?) {

            imageRef.downloadUrl.addOnSuccessListener(object:OnSuccessListener<Uri> {
                override fun onSuccess(p0: Uri?) {
                    **newItemMenu.photoItem = p0.toString()**;
                    //Save menu
                    if ( newItemMenu.save() ){
                        Toast.makeText(activity, "Sucesso ao cadastrar produto!", Toast.LENGTH_LONG).show();
                    }
                }

            }).addOnFailureListener(object: OnFailureListener{
                override fun onFailure(p0: Exception) {
                    Toast.makeText(activity, p0.message, Toast.LENGTH_LONG).show();
                }

            })


        }

    })

Browser other questions tagged

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