Advertisement
Guest User

uploadProfilePicture

a guest
Dec 7th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 1.22 KB | None | 0 0
  1.     private fun uploadProfilePicture(profilePicture: Bitmap, callback: (Exception?, Uri?) -> Unit) {
  2.         Log.v("STORAGE", "Uploading a profile picture")
  3.         val baos = ByteArrayOutputStream()
  4.         val storage = FirebaseStorage.getInstance()
  5.  
  6.         val filePath = "profilePictures/${auth.currentUser!!.uid}"
  7.  
  8.         val storageRef = storage
  9.                 .reference
  10.                 .child(filePath)
  11.         profilePicture.compress(Bitmap.CompressFormat.JPEG, 100, baos)
  12.         val img = baos.toByteArray()
  13.  
  14.         storageRef.putBytes(img).let {
  15.             it.continueWithTask { task ->
  16.                 if (!task.isSuccessful) {
  17.                     task.exception?.let { e ->
  18.                         callback(e, null)
  19.                     }
  20.                 }
  21.                 storageRef.downloadUrl
  22.             }.addOnCompleteListener{ task ->
  23.                 if (task.isSuccessful) {
  24.                     Log.v("STORAGE", "Profile picture upload successful")
  25.                     callback(null, task.result)
  26.                 } else {
  27.                     Log.e("STORAGE", "Profile picture upload failed")
  28.                     callback(task.exception, null)
  29.                 }
  30.             }
  31.         }
  32.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement