I am developing an application in which I can choose a background for a recyclerview (an imageview behind a recyclerview). After selecting the image from the gallery, I would like to send it to the firebase. However when uploading the image my app is freezing and I can't scroll the recyclerview
My code:
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (resultCode == Activity.RESULT_OK) {
/* code */
else if (requestCode == BG_GALLERY_REQUEST_CODE) {
val selectedPicture = data?.data
val filePathColumn = arrayOf(MediaStore.Images.Media.DATA)
val cursor = applicationContext.contentResolver.query(selectedPicture!!, filePathColumn, null, null, null)
cursor!!.moveToFirst()
val columnIndex = cursor.getColumnIndex(filePathColumn[0])
val picturePath = cursor.getString(columnIndex)
val bitmap = BitmapFactory.decodeFile(picturePath)
cursor.close()
backgroundUtils.salvarBackground(bitmap)
val byteArrayOutputStream = ByteArrayOutputStream()
bitmap?.compress(Bitmap.CompressFormat.JPEG, 80, byteArrayOutputStream)
val dadosImagem = byteArrayOutputStream.toByteArray()
val storageReference = ConfiguracaoFirebase.getFirebaseStorage()
val imagemRef = storageReference
.child("imagens")
.child("backgrounds")
.child(UsuarioFirebase.getId() +
FileUtils.IMAGE_EXTENSION)
val uploadTask = imagemRef.putBytes(dadosImagem)
uploadTask.addOnSuccessListener {
imagemRef.downloadUrl.addOnSuccessListener { uri ->
ConfiguracaoFirebase.backgroundRef()
.child(UsuarioFirebase.getId())
.setValue(uri)
Log.e("upload", uri.toString())
}
}.addOnFailureListener { exception ->
Log.e("upload", exception.message)
}
}
} else if (resultCode == Activity.RESULT_CANCELED) {
Log.i("REQUEST", "canceled")
} else {
toast(getString(R.string.oops_ocorreu_algum_erro))
}
}
I also tried to execute the upload code inside an AsyncTask but the problem persisted. How to solve this error?
Thanks in advance