首页 > 解决方案 > Firebase Storage Emulator Android:下载 URL 错误

问题描述

在 Android 上使用存储模拟器时:

FirebaseStorage.getInstance().apply {
    useEmulator("10.0.2.2", 9199)
}

来自文档:10.0.2.2 是从 Android 模拟器连接到主机的“localhost”的特殊 IP 地址。

并上传文件,然后检索其下载 URL:

private fun getReference() = database.collection(COLLECTION_REPORTS)

private fun getStorageReference() = storage.reference
        .child(COLLECTION_REPORTS)

suspend fun save(item: Report, image: Uri) = withContext(defaultDispatcher) {
        val docRef = getReference().document()
        val imageRef = getStorageReference()
            .child(docRef.id)
            .putFile(image)
            .await()
        val imageUrl = imageRef.storage.downloadUrl.await().toString()

        docRef.set(item.apply { photoUrl = imageUrl })
            .await()
    }

它将使用特殊的 IP 地址 ( 10.0.2.2) 返回下载 URL,而不是localhost

http://10.0.2.2:9199/v0/b/project.appspot.com/o/reports%2Fbn8A9YQ9plGUUGMZjX9i?alt=media&token=cfefb3f9-0c7c-4094-ab98-a8fd62fc233b

此 URL 无效,因为我无法通过 Android 模拟器或 PC 浏览器直接访问该图像。

但如果我在 url 中替换10.0.2.2为,它将起作用。localhost

http://localhost:9199/...rest of the url

不知道是不是BUG,有解决办法吗?

标签: androidfirebase-storagefirebase-tools

解决方案


推荐阅读