android - 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,有解决办法吗?
解决方案
推荐阅读
- git - 我可以在不删除最近提交的情况下删除源/主合并历史记录吗?
- java - 在测试中找不到 Spring Boot Health Endpoint
- c# - FileHelpers - 使用 FixedLengthRecords 进行子类化
- webpack - html-webpack-plugin 将错误的路径写入 src 标签
- php - 如何因不活动而使 Laravel 护照令牌过期
- python - 代码应仅返回 1 时返回数字列表
- android - 我收不到 Firebase 和 Google 开发者邀请电子邮件
- go - 处理动态 JSON 模式解码
- java - 多表 - 缺失数据
- r - 遍历列序列(具有相同的作物相关数据)以在 R 中构建整洁的 rbind 数据集