Reputation: 5
Trying to upload image to firebase storage, already added dependecies, storage is on public, no errors in logcat, user authentification works perfectly
private fun performRegister() {
val email = email_edittext_register.text.toString()
val password = password_edittext_register.text.toString()
if (email.isEmpty() || password.isEmpty()) {
Toast.makeText(this, "Fill fields", Toast.LENGTH_SHORT).show()
return
}
Log.d("RegisterActivity", "email is " + email)
Log.d("RegisterActivity", "password is $password")
FirebaseAuth.getInstance().createUserWithEmailAndPassword(email, password)
.addOnCompleteListener {
if (!it.isSuccessful) return@addOnCompleteListener
Log.d("RegisterActivity", "Succesfully created user with uid: ${it.result?.user?.uid}")
}
.addOnFailureListener {
Log.d("RegisterActivity", "Faild to create user ${it.message}")
Toast.makeText(this, "Faild to create user ${it.message}", Toast.LENGTH_SHORT).show()
}
}
private fun uploadImageToFirebaseStorage(){
if(SelectedPhotoUri==null)return
val filename=UUID.randomUUID().toString()
val ref=FirebaseStorage.getInstance().getReference("/images/$filename")
ref.putFile(SelectedPhotoUri!!)
.addOnSuccessListener {
Log.d("Register","succesfuly uploaded image: ${it.metadata?.path}")
}
}
no errors in logcat
Upvotes: 0
Views: 182
Reputation: 500
To get the image URI start activity for result. It will open an image picker:
val intent = Intent(Intent.ACTION_GET_CONTENT)
intent.type = "image/jpeg"
intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true)
startActivityForResult(
Intent.createChooser(intent, "Complete action using"),
RC_PHOTO_PICKER
)
In onActivityResult check is it result from your image picker.
if (requestCode == RC_PHOTO_PICKER && resultCode == Activity.RESULT_OK) {
pushPicture(data)
}
and at the end method to push the image to Firebase Storage:
fun pushPicture(data: Intent?) {
val selectedImageUri = data!!.data
val imgageIdInStorage = selectedImageUri!!.lastPathSegment!! //here you can set whatever Id you need
storageReference.child(imgageIdInStorage).putFile(selectedImageUri)
.addOnSuccessListener { taskSnapshot ->
val urlTask = taskSnapshot.storage.downloadUrl
urlTask.addOnSuccessListener { uri ->
//do sth with uri
}
}
}
Upvotes: 3