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
- 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 {}