简体   繁体   English

我如何判断它是图像还是视频 - Firebase Storage/Firestore

[英]How can I tell if it's an image or video - Firebase Storage/Firestore

I have several images on Firestore in an array.我在数组中的 Firestore 上有几张图片。 This array is in an object (house).该数组位于 object(房子)中。 The array contains both images which are all in storage.该数组包含两个图像,它们都在存储中。 I only saved the ULS each time.我每次只保存 ULS。 How can I now check whether it is an image or a video from Firestore?我现在如何检查它是来自 Firestore 的图像还是视频? When uploading I use a task Since I use the time when uploading to storage, I can't get the file name either.上传时我用的是task 因为我用的是上传到存储时的时间,所以我也获取不到文件名。

     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()
     }

   }

As @DougStevenson already mentioned in his comment, Firestore doesn't know the type of files you have in the Storage.正如@DougStevenson 在他的评论中已经提到的那样,Firestore 不知道存储中的文件类型。 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.第一个是搜索文件的下载 URL 以查找扩展名,然后相应地处理文件。 Or, instead of storing the URLs in the database in an array, store them in a map:或者,不是将 URL 存储在数据库中的数组中,而是将它们存储在 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.其中键是实际的 URL,值是文件的类型。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将从 Firebase 存储接收的视频字节设置为视频播放器? - How can I set the video bytes received from Firebase storage into a video player? Firebase Firestore iOS:如何将图像保存到 Firestore 中的集合中? - Firebase Firestore iOS: How can I save images to a collection in the firestore? 如何使用Firebase模拟器写入Firestore、Storage? - How to use Firebase Emulator to write to Firestore, Storage? 当我上传图像 firebase 存储 getdownloadUrl() 将下载 url 放入 Firestore Collections 但未在 React JS 中设置 - When I upload a image firebase storage getdownloadUrl() put download url In Firestore Collections but not set it in React JS 如何上传 PDF Firebase 存储? - How can I upload a PDF Firebase Storage? 如何准备 JavaScript 值以存储在 Firebase Firestore 中? - How to prepare a JavaScript value for storage in Firebase Firestore? 如何在 Firebase Firestore 上获取一个字段? - How can I get one field on Firebase Firestore? 如何从 firebase 存储中获取图像 URL 列表并将其上传到云 firestore? - How to get a list of image URL from firebase storage and upload it to cloud firestore? Firebase 云存储规则能否针对 Firestore 数据进行验证? - Can Firebase Cloud Storage rules validate against Firestore data? 如何通过Cloud Functions上传文件到Cloud Storage,并使用Firestore控制对Cloud Storage的访问? - How can I upload files to Cloud Storage through Cloud Functions and use Firestore to control access to Cloud Storage?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM