【问题标题】:What is the constructor for Firebase Storage Reference?Firebase 存储参考的构造函数是什么?
【发布时间】:2021-10-06 16:51:26
【问题描述】:

所以我尝试按照 youtube 上有关 Firebase 存储的教程进行操作,并找到了以下代码:

val filePathAndName="product_images/"+""+timeStamp
val storageReference=FirebaseStorage.getInstance().getReference(filePathAndName)

但上面的代码在教程中使用了 Java,我将其更改为 Kotlin 语法并尝试运行该应用程序,它可以工作。

阅读有关 Firebase 存储的文档后,我发现:

// Create a child reference
// imagesRef now points to "images"
var imagesRef: StorageReference? = storageRef.child("images")

// Child references can also take paths
// spaceRef now points to "images/space.jpg
// imagesRef still points to "images"
var spaceRef = storageRef.child("images/space.jpg")

那么声明引用的正确方法是什么?而filePathAndNamegetReference(filePathAndName)的作用是什么?它在 Kotlin 语法中是否具有与 child 相同的功能?

【问题讨论】:

    标签: android firebase kotlin firebase-storage


    【解决方案1】:

    当您拥有FirebaseStorage 的实例时,您可以在其上调用reference/getReference 以将StorageReference object 获取到根目录,或者(通过传递路径字符串)到特定文件。

    当您有一个StorageReference 对象时,您可以对其调用child(...) 以获取对该引用下某个位置的引用。

    这类似于您在编程的许多其他部分中处理目录的方式:您可以立即将整个路径传递给文件,也可以传递部分路径并以这种方式构建相同的路径。


    creating references 上的文档所示,您可以通过以下方式获得对根目录的引用:

    var storageRef = storage.reference
    

    然后,您可以通过以下方式获取对特定文件的引用:

    var spaceRef = storageRef.child("images/space.jpg")
    

    但是你也可以将上面两行替换成这一行:

    var storageRef = storage.getReference("images/space.jpg")
    

    上述方法具有完全相同的结果,您使用哪一种并没有实际区别(因为这些引用是轻量级对象,尚未调用网络)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-07
      • 1970-01-01
      • 1970-01-01
      • 2020-11-10
      • 2016-10-17
      • 1970-01-01
      • 2015-04-08
      相关资源
      最近更新 更多