Last active
July 25, 2020 14:41
-
-
Save birdeveloper/be5e818c649c0ff4bc42d08f7b4bae95 to your computer and use it in GitHub Desktop.
For more details, visit this link: https://medium.com/@birdeveloper/upload-files-to-android-firebase-stroage-4228fdd8d47f
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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