0

I want to do get reference from any image with random name in Firebase Storage. I set image name with UUID.randomUUID method but I can not reference that image for deleting it before uploading new image. I can delete that image with entering that file original name which is I copied from Firebase Storage. But I need getting reference for file, without entering name manually.

Here is my Firebase Storage and image names

Here is my Kotlin codes

        val uuid = UUID.randomUUID()
        val imageName = "$uuid.jpg"

        val imagereference =
            FirebaseStorage.getInstance().reference.child("images/$cu/profphoto")
                .child(imageName)

        val ref2 = FirebaseStorage.getInstance().reference.child("images/$cu/profphoto/5a91e7f3-144a-4c38-b7b5-36ed644603ca.jpg")

        val db = FirebaseFirestore.getInstance()
        ref2.delete().addOnSuccessListener {


    }
        imagereference.putFile(imageUri!!)
            .addOnSuccessListener {}

UPDATE: Solution

  1. Getting to create imagename in Firestore/Realtime Database documents.

/////

  val uploadPictureReference =
                                    storage.reference.child("images/$cu/profphoto").child(imageName)
                                uploadPictureReference.downloadUrl.addOnSuccessListener {

                            val downloadUrl = it.toString()
                            val auth = FirebaseAuth.getInstance()
                            val cu = auth.currentUser?.uid
                        if (cu != null) {

                            val postMap = hashMapOf<String, Any>()
                            postMap.put("imgName", imageName)
                            postMap.put("downloadUs", downloadUrl)
                            postMap.put("date", Timestamp.now())


                            if ("downloadUs".isEmpty()) {

                                val new = db.collection("Publikusers").document(cu)
                                new.set(postMap, SetOptions.merge())
                                    .addOnSuccessListener {}


 2. Reference to that name
 val ref = db.collection("Publikusers").document(cu!!)
            ref.get().addOnSuccessListener {
                val currentImageName = it.data?.get("imgName")?.toString()
  val ref2 =
                    FirebaseStorage.getInstance().reference.child("images/$cu/profphoto/$currentImageName")

                ref2.delete().addOnSuccessListener {}
  • Yes, Sir. That method adding new image but does not remove the current image. val imageName generates new name but i need reference to current image – terlan abaszade Sep 14 '22 at 10:21
  • Show us the code that uploads the image. – Alex Mamo Sep 14 '22 at 10:33
  • imagereference.putFile(imageUri!!) .addOnSuccessListener {} – terlan abaszade Sep 14 '22 at 10:49
  • Have you checked [this](https://stackoverflow.com/questions/53299915/how-to-get-offline-uploaded-file-download-url-in-firebase/) out? Does it work? – Alex Mamo Sep 14 '22 at 11:59
  • val uploadPictureReference = storage.reference.child("images/$cu/profphoto").child(imageName) uploadPictureReference.downloadUrl.addOnSuccessListener { val downloadUrl = it.toString() val auth = FirebaseAuth.getInstance() val cu = auth.currentUser?.uid Sir i can get downloadUrl from image to database(FIrestore). But i need image reference to remove item from storage – terlan abaszade Sep 14 '22 at 12:06
  • How does the URL look like? – Alex Mamo Sep 14 '22 at 12:09
  • Sir url is downloadUrl which retrieve from Firestore to display it. I can also update the url then downloadUrl changes automatically. But storage does not remove the past – terlan abaszade Sep 14 '22 at 13:15
  • What does the URL look like? Does it start with `https://` or `gs://`? – Alex Mamo Sep 14 '22 at 13:27
  • 1
    Sir, I can found solution to that. Now I am going to update my question. Thanks for your responses – terlan abaszade Sep 14 '22 at 14:17

0 Answers0