Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save birdeveloper/be5e818c649c0ff4bc42d08f7b4bae95 to your computer and use it in GitHub Desktop.
Save birdeveloper/be5e818c649c0ff4bc42d08f7b4bae95 to your computer and use it in GitHub Desktop.
val storageRef = Firebase.storage.reference
fun uploadPhoto(photoUrl: Uri, onComplete: (downloadUrl: String?) -> Unit){
val profilePhotosRef = storageRef.child("photos/${getPref().firebaseUuid}")
val metadata = storageMetadata {
contentType = "image/jpeg"
}
d { "uploadPhoto: $photoUrl" }
val photoFile = photoUrl
val uploadTask = profilePhotosRef.putFile(photoFile,metadata)
uploadTask.addOnProgressListener { taskSnapshot ->
val progress = (100.0 * taskSnapshot.bytesTransferred) / taskSnapshot.totalByteCount
d { "Upload is $progress% done" }
}.addOnPausedListener {
d { "Upload is paused" }
}.addOnFailureListener {
d { "failure: ${it.printStackTrace()}" }
}.addOnSuccessListener {
profilePhotosRef.downloadUrl.addOnSuccessListener {
d { "downloadUrl: $it" }
onComplete(it.toString())
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment