mBinding.attachBtn.setOnClickListener {
mGetContent.launch("image/*")
}
This is the launch code
mGetContent = registerForActivityResult(ActivityResultContracts.GetContent(),
ActivityResultCallback {
val intent = Intent(APP_ACTIVITY, CropperAcrtivity::class.java)
intent.putExtra("DATA", it.toString())
startActivityForResult(intent, 101)
})
Initializing mGetContent
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (resultCode == -1 && requestCode == 101) {
var result = data!!.getStringExtra("RESULT")
var resultUri: Uri = Uri.parse(result)
var bitmap = MediaStore.Images.Media.getBitmap(APP_ACTIVITY.contentResolver, resultUri)
sendMessageAsImage(bitmap)
}
}
Getting the result
The essence of the problem
I'm trying to send images to the chat room, but when I did it through sending URI, the message is written once in the database, but in the Recycler View is displayed 3 times, or the error TransactionTooLargeException: data parcel size 896372 bytes, now I do through bitmap and this error has not gone anywhere, it appears immediately after running "image / *" I do not even have time to choose a photo from the memory
UPD: Sending an image
mBinding.uploadBtn.setOnClickListener {
uploadImageMessage()
}
uploadImageMessage
private fun uploadImageMessage(){
val message = hashMapOf<String, Any>()
message.put(KEY_IS_CHECKED, false)
message.put(SENDER_ID, preferenceManager.getString(KEY_USER_ID)!!)
message.put(RECEIVER_ID, receiverUser.uid)
message.put(KEY_MESSAGE, mBinding.textImages.text)
message.put(KEY_TIMESTAMP, Date())
message.put(KEY_TYPE_MESSAGE, MESSAGE_TYPE_IMAGE)
database.collection(KEY_COLLECTION_USERS)
.whereEqualTo(KEY_UID, preferenceManager.getString(KEY_UID))
.get().addOnCompleteListener {
it.getResult().documents.get(0).reference.collection(KEY_COLLECTION_CHAT)
.add(message)
}
mBinding.frameTypeField.visibility = View.VISIBLE
mBinding.sendBtn.visibility = View.VISIBLE
mBinding.frameAttach.visibility = View.VISIBLE
mBinding.cancelBtn.visibility = View.GONE
mBinding.uploadBtn.visibility = View.GONE
mBinding.iconImage.visibility = View.GONE
mBinding.textImages.visibility = View.GONE
mBinding.typeMessageField.setText(null)
}
sendMessageAsImage
private fun sendMessageAsImage(image: Bitmap) {
mBinding.frameTypeField.visibility = View.GONE
mBinding.sendBtn.visibility = View.GONE
mBinding.frameAttach.visibility = View.GONE
mBinding.cancelBtn.visibility = View.VISIBLE
mBinding.uploadBtn.visibility = View.VISIBLE
mBinding.iconImage.visibility = View.VISIBLE
mBinding.textImages.visibility = View.VISIBLE
mBinding.textImages.text = encodedImage(image).toString()
}
Before I used to send a picture to the database in the sendMessageAsImage function, but because the messages were for some reason displayed three times on the screen I decided to split the function and now sending is done by clicking on uploadBtn