Android ap=hashMapOf() noteMap.put(“downloadUrl”,downloadUrl) noteMap.put(“userEmail”,auth.currentUser!!.email.toString()) noteMap.put(“noteTitle”,titleText.text.toString()) noteMap.put(“yourNote”,noteText.text.toString()) noteMap.put(“日期”,Timestamp.now()) db.collection(“Notes”).add(noteMap).addOnCompleteListener{task-> if(task.isComplete&&task.issucessful){ 完成() } }.addOnFailureListener{异常-> Toast.makeText( 应用上下文, 异常.localizedMessage?.toString(), 吐司,长度 ).show() } } } } }
} 我还尝试添加下面的代码,但没有成功。我该怎么办Android ap=hashMapOf() noteMap.put(“downloadUrl”,downloadUrl) noteMap.put(“userEmail”,auth.currentUser!!.email.toString()) noteMap.put(“noteTitle”,titleText.text.toString()) noteMap.put(“yourNote”,noteText.text.toString()) noteMap.put(“日期”,Timestamp.now()) db.collection(“Notes”).add(noteMap).addOnCompleteListener{task-> if(task.isComplete&&task.issucessful){ 完成() } }.addOnFailureListener{异常-> Toast.makeText( 应用上下文, 异常.localizedMessage?.toString(), 吐司,长度 ).show() } } } } },android,android-studio,kotlin,Android,Android Studio,Kotlin,} 我还尝试添加下面的代码,但没有成功。我该怎么办 fun saveClick(view: View) { //UUID -> Image Name val uuid = UUID.randomUUID() val imageName = "$uuid.jpg" val storage = FirebaseStorage.getInstance() val reference = storage.reference
fun saveClick(view: View) {
//UUID -> Image Name
val uuid = UUID.randomUUID()
val imageName = "$uuid.jpg"
val storage = FirebaseStorage.getInstance()
val reference = storage.reference
val imagesReference = reference.child("images").child(imageName)
if (selectedPicture != null) {
imagesReference.putFile(selectedPicture!!).addOnSuccessListener { taskSnapshot ->
// take the picture link to save the database
val uploadedPictureReference =
FirebaseStorage.getInstance().reference.child("images").child(imageName)
uploadedPictureReference.downloadUrl.addOnSuccessListener { uri ->
val downloadUrl = uri.toString()
println(downloadUrl)
val noteMap = hashMapOf<String, Any>()
noteMap.put("downloadUrl", downloadUrl)
noteMap.put("userEmail", auth.currentUser!!.email.toString())
noteMap.put("noteTitle", titleText.text.toString())
noteMap.put("yourNote", noteText.text.toString())
noteMap.put("date", Timestamp.now())
db.collection("Notes").add(noteMap).addOnCompleteListener { task ->
if (task.isComplete && task.isSuccessful) {
finish()
}
}.addOnFailureListener { exception ->
Toast.makeText(
applicationContext,
exception.localizedMessage?.toString(),
Toast.LENGTH_LONG
).show()
}
}
}
}else {
val noteMap = hashMapOf<String, Any>()
noteMap.put("userEmail", auth.currentUser!!.email.toString())
noteMap.put("noteTitle", titleText.text.toString())
noteMap.put("yourNote", noteText.text.toString())
noteMap.put("date", Timestamp.now())
db.collection("Notes").add(noteMap).addOnCompleteListener { task ->
if (task.isComplete && task.isSuccessful) {
finish()
}
}
}
}
fun saveClick(视图:视图){
//UUID->图像名称
val uuid=uuid.randomUUID()
val imageName=“$uuid.jpg”
val storage=FirebaseStorage.getInstance()
val reference=storage.reference
val imagesReference=reference.child(“图像”).child(图像名称)
如果(selectedPicture!=null){
imagesReference.putFile(selectedPicture!!).addOnSuccessListener{taskSnapshot->
//拍摄图片链接以保存数据库
val上传图片参考=
FirebaseStorage.getInstance().reference.child(“图像”).child(imageName)
uploadedPictureReference.DownloadeUrl.addOnSuccessListener{uri->
val downloadUrl=uri.toString()
println(下载URL)
val noteMap=hashMapOf()
noteMap.put(“downloadUrl”,downloadUrl)
noteMap.put(“userEmail”,auth.currentUser!!.email.toString())
noteMap.put(“noteTitle”,titleText.text.toString())
noteMap.put(“yourNote”,noteText.text.toString())
noteMap.put(“日期”,Timestamp.now())
db.collection(“Notes”).add(noteMap).addOnCompleteListener{task->
if(task.isComplete&&task.issucessful){
完成()
}
}.addOnFailureListener{异常->
Toast.makeText(
应用上下文,
异常.localizedMessage?.toString(),
吐司,长度
).show()
}
}
}
}否则{
val noteMap=hashMapOf()
noteMap.put(“userEmail”,auth.currentUser!!.email.toString())
noteMap.put(“noteTitle”,titleText.text.toString())
noteMap.put(“yourNote”,noteText.text.toString())
noteMap.put(“日期”,Timestamp.now())
db.collection(“Notes”).add(noteMap).addOnCompleteListener{task->
if(task.isComplete&&task.issucessful){
完成()
}
}
}
}
}因此,您的所有逻辑基本上都包含在
putFile
成功回调中,这要求在将任何内容添加到数据库之前成功存储和检索图像(?)
你需要打破这种逻辑,这样你就只能运行你想要的部分——比如如果你没有一个映像,就只运行数据库更新部分,或者在成功处理映像之后再运行那个部分
所以,实际上,您需要一个“存储在数据库中”函数来处理最终的写入-如果没有图像,直接调用它,如果有图像,则在成功回调中调用它。我只是想对其进行伪编码,但是:
saveData(noteMap: Map) {
add note to DB
}
saveClick() {
create noteMap with basic, non-image details (email, title etc)
if image is null, call saveData(noteMap)
else do the image stuff:
onSuccess:
add the downloadUrl to noteMap
call saveData(noteMap)
}
我看到你已经用一个else分支进行了编辑,它创建了地图sans url并写入了它-你基本上就快到了,只是把它变成一个函数,然后把地图传递进来 那么什么“不起作用”?您是否收到错误?没有错误,但应用程序已关闭,ı必须删除所有数据库才能重新打开againHi,感谢您的解释。我试过了,但我想我错过了什么。
saveData(noteMap: Map) {
add note to DB
}
saveClick() {
create noteMap with basic, non-image details (email, title etc)
if image is null, call saveData(noteMap)
else do the image stuff:
onSuccess:
add the downloadUrl to noteMap
call saveData(noteMap)
}