Skip to content

Instantly share code, notes, and snippets.

@AdoraNwodo
Last active August 4, 2021 06:27
Show Gist options
  • Save AdoraNwodo/72e24b06ebb6526328e64ca8393e7c2e to your computer and use it in GitHub Desktop.
Save AdoraNwodo/72e24b06ebb6526328e64ca8393e7c2e to your computer and use it in GitHub Desktop.
private fun uploadImage(){
if(filePath != null){
val ref = storageReference?.child("uploads/" + UUID.randomUUID().toString())
val uploadTask = ref?.putFile(filePath!!)
val urlTask = uploadTask?.continueWithTask(Continuation<UploadTask.TaskSnapshot, Task<Uri>> { task ->
if (!task.isSuccessful) {
task.exception?.let {
throw it
}
}
return@Continuation ref.downloadUrl
})?.addOnCompleteListener { task ->
if (task.isSuccessful) {
val downloadUri = task.result
addUploadRecordToDb(downloadUri.toString())
} else {
// Handle failures
}
}?.addOnFailureListener{
}
}else{
Toast.makeText(this, "Please Upload an Image", Toast.LENGTH_SHORT).show()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment