Home > database >  How can I tell if it's an image or video - Firebase Storage/Firestore
How can I tell if it's an image or video - Firebase Storage/Firestore

Time:12-31

I have several images on Firestore in an array. This array is in an object (house). The array contains both images which are all in storage. I only saved the ULS each time. How can I now check whether it is an image or a video from Firestore? When uploading I use a task Since I use the time when uploading to storage, I can't get the file name either.

     Tasks.whenAllSuccess<UploadTask>(tasks).addOnSuccessListener {

        val downloadUrls = mutableListOf<String>()

        var count = 0

        for(i in 0 until tasks.size){
         tasks[i].result.metadata!!.reference!!.downloadUrl.addOnSuccessListener {uri->

                downloadUrls.add(uri.toString())
                count  

             if(count == tasks.size)
             {
                 progressDialog.dismiss()

                 saveInDatabase(downloadUrls)
             }


         }

          }
        
    }



   private fun saveInDatabase(downloadUrls: MutableList<String>) {

    var time = System.currentTimeMillis().toString()

    val userHashMap = HashMap<String,Any>()


    if(downloadUrls.isNotEmpty())
    {

        userHashMap["timestamp"] = time
        userHashMap["type"] = mainType
        userHashMap["img"] = mainImage
        userHashMap["preis"] = 845
        userHashMap["listImages"] = downloadUrls

    }

    if(latt != 0.0 && longt != 0.0)
    {
        userHashMap["long"] = longt
        userHashMap["lat"] = latt
    }

    if(objektBeschreibung.text.toString().isNotEmpty())
    {
        userHashMap["beschreibung"] = objektBeschreibung.text.toString()
    }

    if(objektName.text.toString().isNotEmpty())
    {
        userHashMap["name"] = objektName.text.toString()
    }

    db.collection(  "USER").document(auth.currentUser!!.uid).collection("Objekte").document(time).set(userHashMap, SetOptions.merge()).addOnSuccessListener {
        Toast.makeText(ctx,"Erfolgreich angelegt",Toast.LENGTH_LONG).show()
     }

   }

CodePudding user response:

As @DougStevenson already mentioned in his comment, Firestore doesn't know the type of files you have in the Storage. To know that, you have two solutions. The first one would be to search the download URL of the file for the extension and then process the file accordingly. Or, instead of storing the URLs in the database in an array, store them in a map:

$docId (documents)
  |
  --- urls (map)
       |
       --- "https://...": "audio"
       |
       --- "https://...": "video"

Where the key is the actual URL and the value is the type of the file.

  • Related