您好,我是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()
}
}
}
})
}
}
}
}
}
我正在尝试为用户添加到应用程序的每个项目保存保存的图像数组
答案 0 :(得分:0)
私人乐趣AddItem(){
val itemName: String = title!!.text.toString()
val itemPrice: String = price!!.text.toString()
val itemDescription: String = description!!.text.toString()
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(itemName) -> Toast.makeText(
activity,
"Title is required.",
Toast.LENGTH_LONG
).show()
TextUtils.isEmpty(itemPrice.toString()) -> Toast.makeText(
activity,
"Price is required.",
Toast.LENGTH_LONG
).show()
TextUtils.isEmpty(itemDescription) -> Toast.makeText(
activity,
"Please write a short description of your item",
Toast.LENGTH_LONG
).show()
else -> {
var uploadTask: StorageTask<UploadTask.TaskSnapshot>
progressBar!!.visibility = view!!.visibility
while (up < uploadedImages.size) {
val itemRef: StorageReference = storageItemPicRef!!.child(firebaseUser.uid)
.child(uploadedImages[k].lastPathSegment!!)
uploadTask = itemRef.putFile(uploadedImages[k])
.addOnSuccessListener { p0 ->
val downloadUrl =
p0!!.storage.downloadUrl
}.addOnFailureListener {
}
up++
k++
uploadTask.continueWithTask(Continuation<UploadTask.TaskSnapshot, Task<Uri>> { task ->
if (!task.isSuccessful) {
task.exception?.let {
throw it
}
}
return@Continuation itemRef.downloadUrl
}).addOnCompleteListener (OnCompleteListener { task ->
if (task.isSuccessful) {
downloadImages = ArrayList()
val downloadUrl = task.result
(downloadImages as ArrayList<Uri>).add(downloadUrl!!)
val itemMap = HashMap<String, Any>()
doc = FirebaseDatabase.getInstance().reference.child("Items")
.child(firebaseUser.uid)
for(element in downloadImages) {
/* mPhotos.put(doc.push().key!!, downloadImages[i].toString())*/
mPhotos.add(element.toString())
}
itemMap["uid"] = firebaseUser.uid
itemMap["state"] = mState
itemMap["title"] = itemName
itemMap["description"] = itemDescription
itemMap["price"] = itemPrice
itemMap["images"] = mPhotos
itemMap["lga"] = mLga
itemMap["category"] = mCategory
itemMap["subcategory"] = mSubcategory
doc.setValue(itemMap)
sendToHomeActivity()
progressBar!!.visibility = View.INVISIBLE
Toast.makeText(activity, "Item has been created successfully", Toast.LENGTH_LONG).show()
}
})
}
}
}
}
private fun sendToHomeActivity() {
val mainIntent = Intent(context, HomeActivity::class.java)
startActivity(mainIntent)
}