我需要通过使用图库上传图像并将其保存到火灾存储中。取而代之的是,我想将可下载的链接获取到实时数据库中。帮助我修复此代码。所有其他数据均已更新为实时数据库,没有任何问题。
class ActivityAddPost : AppCompatActivity() {
private val PICK_IMAGE_REQUEST = 1
lateinit var mUri: Uri
lateinit var title: String
lateinit var description: String
lateinit var location: String
lateinit var price: String
lateinit var contact: String
private var mFirebaseStore: FirebaseStorage? = null
private lateinit var mDatabaseReference: DatabaseReference
private var mStorageReference: StorageReference? = null
private lateinit var mFirebaseAuth: FirebaseAuth
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_create_post)
val btnContinue = findViewById<Button>(R.id.buttonContinue)
mDatabaseReference =
FirebaseDatabase.getInstance().reference.child("Adds")
mFirebaseStore = FirebaseStorage.getInstance()
mStorageReference = FirebaseStorage.getInstance().reference
btnContinue.setOnClickListener {
pushAdd()
uploadImage()
}
imageView.setOnClickListener { launchGallery() }
}
private fun launchGallery() {
val intent = Intent(Intent.ACTION_PICK)
intent.type = "image/*"
startActivityForResult(Intent.createChooser(intent, "Select Picture"), 1)
}
private fun pushAdd() {
title = txt1.text.toString()
description = txt2.text.toString()
location = txt3.text.toString()
price = txt4.text.toString()
contact = txt5.text.toString()
val addInfo = HashMap<String, Any>()
addInfo.put("Title", title)
addInfo.put("Description", description)
addInfo.put("Location", location)
addInfo.put("Price", price)
addInfo.put("Contact", contact)
val addId = mDatabaseReference.push().key
if (addId != null) {
mDatabaseReference.child(addId).setValue(addInfo)
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == PICK_IMAGE_REQUEST && resultCode == Activity.RESULT_OK && null != data) {
mUri = data.data!!
try {
val bitmap = MediaStore.Images.Media.getBitmap(contentResolver, mUri)
val baos = ByteArrayOutputStream()
bitmap.compress(Bitmap.CompressFormat.JPEG, 20, baos)
val data = baos.toByteArray()
mStorageReference?.putBytes(data)
} catch (e: IOException) {
e.printStackTrace()
}
Glide
.with(this)
.load(mUri)
.centerCrop()
.into(imageView)
}
}
private fun addUploadRecordToDb(uri: String) {
val db = FirebaseFirestore.getInstance()
val data = HashMap<String, Any>()
data["imageUrl"] = uri
db.collection("posts")
.add(data)
.addOnSuccessListener { documentReference ->
Toast.makeText(this, "Saved to DB", Toast.LENGTH_LONG).show()
}
.addOnFailureListener { e ->
Toast.makeText(this, "Error saving to DB", Toast.LENGTH_LONG).show()
}
}
private fun uploadImage() {
if (mUri != null) {
val ref = mStorageReference?.child("Images/" + UUID.randomUUID().toString())
val uploadTask = ref?.putFile(mUri!!)
val urlTask =
uploadTask?.continueWithTask(Continuation<UploadTask.TaskSnapshot, Task<Uri>> { task ->
if (!task.isSuccessful) {
task.exception?.let {
throw it
}
}
return@Continuation ref.downloadUrl
})?.addOnCompleteListener { task ->
if (task.isSuccessful) {
val downloadUri = task.result
addUploadRecordToDb(downloadUri.toString())
} else {
}
}?.addOnFailureListener {
}
} else {
Toast.makeText(this, "Please Upload an Image", Toast.LENGTH_SHORT).show()
}
}
}