Home » Android » android – Kotlin – download url for image from firebase storage-Exceptionshub

android – Kotlin – download url for image from firebase storage-Exceptionshub

Posted by: admin February 26, 2020 Leave a comment

Questions:

I am having problems downloading a url from firebase storage for a basic messaging app, below is my progress so far, however I believe I am trying to use outdated dependencies and am unsure how to fix this as I am very new to kotlin.

createSnapImageView?.isDrawingCacheEnabled = true
        createSnapImageView?.buildDrawingCache()
        val bitmap = (createSnapImageView?.drawable as BitmapDrawable).bitmap
        val baos = ByteArrayOutputStream()
        bitmap?.compress(Bitmap.CompressFormat.JPEG, 100, baos)
        val data = baos.toByteArray()

        var uploadTask =
            FirebaseStorage.getInstance().reference.child("images").child(imageName)
                .putBytes(data)
        uploadTask
            .addOnFailureListener {
                // Handle unsuccessful uploads
                Toast.makeText(this, "Upload Failed", Toast.LENGTH_SHORT).show()
            }
            .addOnSuccessListener(OnSuccessListener<UploadTask.TaskSnapshot>
            { taskSnapshot ->
                // taskSnapshot.getMetadata() contains file metadata such as size, content-type, and download URL.
                val downloadUrl = taskSnapshot.metadata?.reference?.downloadUrl
                val intent = Intent(this, ChooseUserActivity::class.java)
                intent.putExtra("imageURL", downloadUrl.toString())
                intent.putExtra("imageName", imageName)
                intent.putExtra("message", messageEditText?.text.toString())
                startActivity(intent)
            }).addOnSuccessListener {
                OnSuccessListener<UploadTask.TaskSnapshot> {
                }
            }
How to&Answers: