Trying to place an image in firebase storage but it does not seem to work.
I have tried to change the rules and make sure that the dependencies are on and also applied the storage permission needed but it doesnt seem to work.
The following is the code that is used to authenticate a user and upload their profile image, the authentication works however the uploading does not.
var selectedPhotoUri: Uri? = null
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == 0 && resultCode == Activity.RESULT_OK && data != null && data.data != null) {
// proceed and check what the selected image was....
Log.d(TAG, "Photo was selected")
selectedPhotoUri = data.data
val bitmap = MediaStore.Images.Media.getBitmap(contentResolver, selectedPhotoUri)
selectphoto_imageview_register.setImageBitmap(bitmap)
selectphoto_button_register.alpha = 0f
}
}
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, "Please enter text in email/pw", Toast.LENGTH_SHORT).show()
return
}
Log.d(TAG, "Attempting to create user with email: $email")
// Firebase Authentication to create a user with email and password
FirebaseAuth.getInstance().createUserWithEmailAndPassword(email, password)
.addOnCompleteListener {
if (!it.isSuccessful) return@addOnCompleteListener
// else if successful
Log.d(TAG, "Successfully created user with uid: ${it.result!!.user.uid}")
uploadImageToFirebaseStorage()
}
.addOnFailureListener{
Log.d(TAG, "Failed to create user: ${it.message}")
Toast.makeText(this, "Failed to create user: ${it.message}", Toast.LENGTH_SHORT).show()
}
}
private fun uploadImageToFirebaseStorage() {
if (selectedPhotoUri != null){
val progressDialog = ProgressDialog(this)
progressDialog.setTitle("Uploading...")
progressDialog.show()
val filename = UUID.randomUUID().toString()
val ref = FirebaseStorage.getInstance().getReference("/images/$filename")
Log.d(TAG, "this is the data $selectedPhotoUri")
ref.putFile(selectedPhotoUri!!)
.addOnSuccessListener {
Log.d(TAG, "Successfully uploaded image: ${it.metadata?.path}")
progressDialog.hide()
ref.downloadUrl.addOnSuccessListener {
Log.d(TAG, "File Location: $it")
saveUserToFirebaseDatabase(it.toString())
}
}
.addOnFailureListener {
Log.d(TAG, "Failed to upload image to storage: ${it.message}")
}
.addOnProgressListener {taskSnapshot ->
val progress = 100.0 * taskSnapshot.bytesTransferred/taskSnapshot.totalByteCount
progressDialog.setMessage("Uploaded " +progress.toInt())
}
}
all I am left of when I press the register button is the progress Dialog screen that doesn't end. the performRegister function is executed in the onCreate function. There is no error code that comes out and I am not sure what is causing the issue. The storage rules are public so I dont think that is the problem either.