【问题标题】:How to Upload Multiple images to FiresbaseStorage and get back the urls in JetpackCompose如何将多个图像上传到 Firebase 存储并在 Jetpack Compose 中取回 URL
【发布时间】:2022-11-19 01:25:07
【问题描述】:

我从 Alex Mamo How to upload an image to Firebase 那里得到了一个想法,如何将图像上传到 Firebase 存储并取回 URL 并使用 MVVM 和 Hilt 依赖项上传到 Firestore,但如何将图像 URI 的 ArrayList 上传到存储并取回 URL。

我正在从我的 ViewModel 中的图库中获取 Selected Images Uri

    fun updateSelectedImageList(listOfImages: List<Uri>) {
    val updatedImageList = state.productImagesList.toMutableList()
    viewModelScope.launch {
        updatedImageList += listOfImages
        state = state.copy(
            productImagesList = updatedImageList.distinct()
        )
    }
}

如果我对 Uri 图片列表的回答有误,请纠正我

资料库

typealias AddCategoryResponse = Response<Boolean>
typealias AddContentUriResponse = Response<Uri>
typealias AddProductImagesResponse = Response<ProductImages>

suspend fun addProductImagesToFirebaseStorage(productImages: List<Uri>) : AddProductImagesResponse

suspend fun addMainCategoryImageToFirebaseStorage(imageUri: Uri,upcomingCat: Int) : AddContentUriResponse

suspend fun addMainCategoryToFirestore(mainCategory: MainCategory) : AddCategoryResponse

我想要一个创建函数来添加多个图像并取回更新的图像 url 返回方法

我的实现

@Singleton
class AdminRepositoryImpl @Inject constructor(
@Named("mainCategory")
private val categoryRef: CollectionReference,
@Named("product")
private val productRef: CollectionReference,
@Named("tags")
private val tagsRef: CollectionReference,

private val categoryImageStorage: FirebaseStorage,
) : AdminRepository {

override suspend fun addProductImagesToFirebaseStorage(productImages: List<Uri>): 
AddProductImagesResponse {
    return try {
        val date = System.currentTimeMillis()
        val productDownloadUrls: List<URL> = emptyList()
        productDownloadUrls = //Need to get Success Response of the List Images
  

    categoryImageStorage.reference.child("HomeFeed")
   .child("Products")
            .child("Products$date")

    }
}

override suspend fun addMainCategoryImageToFirebaseStorage(
    imageUri: Uri, upcomingCat: Int,
): AddContentUriResponse {
    return try {
       
            val date = System.currentTimeMillis()
            val downloadUrl =
                categoryImageStorage.reference.child("HomeFeed").child("SubCategory")
                    .child("SubCategoryImage$date")
                    .putFile(imageUri).await()
                    .storage.downloadUrl.await()
            Success(downloadUrl)
        }

    } catch (e: Exception) {
        Failure(e)
    }
}

取回上传图片的url

@Composable
fun AddCategoryImageToStorage(
viewModel: CategoryViewModel = hiltViewModel(),
addCategoryImageToStorage : (downloadUrl: Uri) -> Unit
) {
when(val addCategoryImageToStorageResponse = 
viewModel.addCategoryImageToStorageResponse){
    is Response.Loading -> ProgressBar()
 is Response.Success -> addCategoryImageToStorageResponse.data?.let{ downloadUrl - >
        LaunchedEffect(downloadUrl){
            addCategoryImageToStorage(downloadUrl)
        }
    }
    is Response.Failure -> LaunchedEffect(Unit){
        print(addCategoryImageToStorageResponse.e)
    }
  }

}

用例也被使用

【问题讨论】:

    标签: android firebase google-cloud-platform google-cloud-firestore firebase-storage


    【解决方案1】:

    我实际上写了那篇文章,是的,它只解释了如何上传单个文件。但是上传多个文件的机制几乎是一样的,除了你应该等待上传所有图片。当您在 StorageReference 对象上调用 putFile(Uri uri) 方法时,如下所示:

    categoryImageStorage.reference.child("HomeFeed")
                                  .child("SubCategory")
                                  .child("SubCategoryImage$date")
                                  .putFile(imageUri) //?
    

    返回的对象类型是UploadTask。这个类是StorageTask的子类,它是ControllableTask的子类,也是CancellableTask的子类,最后是Task的子类。由于这些类之间的继承关系,每次上传文件时都可以调用await()。由于您需要上传多个文件,因此您需要添加所有产生列表的任务对象。一旦列表中充满了对象,将该列表传递给Task#whenAllSuccess(Collection> tasks)方法。此方法返回类型为Task&lt;List&lt;TResult&gt;&gt; 的对象。也就是说,您可以调用 await() 来等待上传操作完成。

    如果上面的操作不够快,请查看我在下面帖子中的回答:

    【讨论】:

      猜你喜欢
      • 2019-03-24
      • 1970-01-01
      • 1970-01-01
      • 2021-04-07
      • 2020-07-27
      • 2019-07-24
      • 2021-12-27
      • 1970-01-01
      • 2017-12-03
      相关资源
      最近更新 更多