将多个图像上传到Firebase存储并将下载URL保存到android studio kotlin中的实时数据库中

问题描述 投票:1回答:1

你好,我是android开发的新手,我正试图将多个图像上传到Firebase实时数据库中>]

我正在构建的应用是电子商务应用。这是下面的我的addItem活动

// onActivity结果

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)
    if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE && resultCode ==
        Activity.RESULT_OK && data != null


    ) {


        val result = CropImage.getActivityResult(data)
        imageUri1 = result.uri
        goodsimage1?.setImageURI(imageUri1)
        uploadedImages.add(imageUri1!!)


    }
    if (requestCode == 2 && resultCode ==
        Activity.RESULT_OK && data != null
    ) {
        val result = CropImage.getActivityResult(data)
        imageUri2 = result.uri
        goodsimage2?.setImageURI(imageUri2)
        uploadedImages.add(imageUri2!!)


    }
    if (requestCode == 3 && resultCode ==
        Activity.RESULT_OK && data != null
    ) {
        val result = CropImage.getActivityResult(data)
        imageUri3 = result.uri
        goodsimage3?.setImageURI(imageUri3)
        uploadedImages.add(imageUri3!!)

    }
    if (requestCode == 4 && resultCode ==
        Activity.RESULT_OK && data != null
    ) {
        val result = CropImage.getActivityResult(data)
        imageUri4 = result.uri
        goodsimage4?.setImageURI(imageUri4)
        uploadedImages.add(imageUri4!!)

    }
    if (requestCode == 5 && resultCode ==
        Activity.RESULT_OK && data != null
    ) {
        val result = CropImage.getActivityResult(data)
        imageUri5 = result.uri
        goodsimage5?.setImageURI(imageUri5)
        uploadedImages.add(imageUri5!!)

    }


}

// addItem活动

    when {


        imageUri1 == null && imageUri2 == null && imageUri3 == null && imageUri4 == null && imageUri5 == null ->
            Toast.makeText(
                activity,
                "Please add at least one image.",
                Toast.LENGTH_LONG
            ).show()

        TextUtils.isEmpty(itemTitle) -> Toast.makeText(
            activity,
            "Title is required.",
            Toast.LENGTH_LONG
        ).show()
        TextUtils.isEmpty(itemPrice) -> Toast.makeText(
            activity,
            "Price is required.",
            Toast.LENGTH_LONG
        ).show()
        TextUtils.isEmpty(mState) -> Toast.makeText(
            activity,
            "State is required.",
            Toast.LENGTH_LONG
        ).show()
        TextUtils.isEmpty(mLga) -> Toast.makeText(
            activity,
            "LGA is required.",
            Toast.LENGTH_LONG
        ).show()
        TextUtils.isEmpty(mCategory) -> Toast.makeText(
            activity,
            "Category is required.",
            Toast.LENGTH_LONG
        ).show()
        TextUtils.isEmpty(mSubcategory) -> Toast.makeText(
            activity,
            "Subcategory is required.",
            Toast.LENGTH_LONG
        ).show()

        else -> {
            val progressDialog = ProgressDialog(activity)
            progressDialog.setTitle("Adding an item")
            progressDialog.setMessage("Please wait, while we add your item....")
            progressDialog.setCanceledOnTouchOutside(false)
            progressDialog.show()


            var uploadTask: StorageTask<UploadTask.TaskSnapshot>

            var up = 0


            while(up < uploadedImages.size) {
                val perFile : Uri = uploadedImages.get(up)


                val photoRef: StorageReference = storageItemPicRef!!.child(uploadedImages.get(up).lastPathSegment!!)


                uploadTask = photoRef.putFile(perFile)

                uploadTask.continueWithTask(Continuation<UploadTask.TaskSnapshot, Task<Uri>> { task ->
                    if (!task.isSuccessful) {
                        task.exception?.let {
                            throw it
                            progressDialog.dismiss()
                        }

                    }


                    return@Continuation photoRef.downloadUrl


                }).addOnCompleteListener(OnCompleteListener<Uri> { task ->
                    if (task.isSuccessful) {

                    mPhotos = ArrayList()
                        mPhotos.clear()


                        val downloadUrl = task.result
                      /*  myUrl = downloadUrl.toString()*/


                        mPhotos.add(downloadUrl!!.toString())


                        val doc = fStore.reference.child("Items")

                        val itemId = doc.push().key
                        val itemMap = HashMap<String, Any>()

                        itemMap["title"] = itemTitle
                        itemMap["price"] = itemPrice
                        itemMap["description"] = itemDescription
                        itemMap["category"] = mCategory
                        itemMap["subcategory"] = mSubcategory
                        itemMap["state"] = mState
                        itemMap["lga"] = mLga
                        itemMap["image"] = mPhotos

                        itemMap["itemId"] = itemId!!

                             doc.child(itemId).setValue(itemMap)
                                 .addOnCompleteListener { task ->
                                     if (task.isSuccessful) {

                                         Toast.makeText(
                                             activity,
                                             "Item has been created successfully!",
                                             Toast.LENGTH_LONG
                                         ).show()
                                         progressDialog.dismiss()
                                         val intent =
                                             Intent(activity, HomeActivity::class.java)

                                         startActivity(intent)
                                         activity!!.finish()


                                     } else {
                                         progressDialog.dismiss()
                                         val message = task.exception.toString()
                                         Toast.makeText(
                                             activity,
                                             "Error: $message",
                                             Toast.LENGTH_LONG
                                         )
                                             .show()

                                     }
                                 }
                    }
                })
            }
        }
    }
}
}

我正在尝试为用户添加到应用程序的每个项目保存保存的图像阵列

[你好,我是android开发的新手,我正在尝试将多个图像上传到Firebase实时数据库,我正在构建的应用程序是一个电子商务应用程序。这是我的addItem活动,// // ...

database firebase firebase-storage
1个回答
0
投票

私人乐趣AddItem(){

© www.soinside.com 2019 - 2024. All rights reserved.