title: Save User to Firebase Database in Kotlin | Android Studio
body: <p>My problem is that I want save the uid, username and his profileImageUrl. All variables give the output that I need except &quot;user&quot; (I think).</p>
  <p><strong>This is the function to save the user to database:</strong></p>
  <pre><code>private fun saveUserToFirebaseDatabase(profileImageUrl: String) {
                  val uid = FirebaseAuth.getInstance().uid ?: &quot;&quot;
                  val ref = FirebaseDatabase.getInstance().getReference(&quot;/users/$uid&quot;)
  
                  val user = User(uid, username_edittext_register.text.toString(), profileImageUrl)
  
                  ref.setValue(user)
                          .addOnSuccessListener {
                                  Log.d(&quot;RegisterActivity&quot;, &quot;Finally we saved the user to Firebase Database&quot;)
  
                                  val intent = Intent(this,LatestMessagesActivity::class.java)
                                  intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TASK.or(Intent.FLAG_ACTIVITY_NEW_TASK)
                                  startActivity(intent)
                          }
                          .addOnFailureListener{
                                  Log.d(&quot;RegisterActivity&quot;, &quot;Failed to set value to database: ${it.message}&quot;)
                          }
                          Log.d(&quot;saveUserToDatabase&quot;,&quot;Task completed? User: $user&quot;)
          }
  </code></pre>
  <p><strong>This the class User:</strong></p>
  <pre><code>class User(val uid: String,val username: String,val profileImageUrl: String) {
          constructor() : this(&quot;&quot;, &quot;&quot;, &quot;&quot;)
  }
  </code></pre>
  <p><strong>This is the &quot;whole&quot; code:</strong></p>
  <pre><code>package letsbuildthatapp.com
  
  import android.app.Activity
  import android.content.ContentValues.TAG
  import android.content.Intent
  import android.graphics.drawable.BitmapDrawable
  import android.net.Uri
  import android.os.Bundle
  import android.provider.MediaStore
  import android.util.Log
  import android.widget.Toast
  import androidx.appcompat.app.AppCompatActivity
  import com.google.firebase.auth.FirebaseAuth
  import com.google.firebase.database.FirebaseDatabase
  import com.google.firebase.storage.FirebaseStorage
  import kotlinx.android.synthetic.main.activity_register.*
  import java.util.*
  
  
  class RegisterActivity : AppCompatActivity() {
          override fun onCreate(savedInstanceState: Bundle?) {
                  super.onCreate(savedInstanceState)
                  setContentView(R.layout.activity_register)
  
                  register_button_register.setOnClickListener {
                          performRegister()
                  }
  
                  already_have_account_text_view.setOnClickListener {
                          Log.d(&quot;RegisterActivity&quot;, &quot;Try to show login activity&quot;)
  
                          //Launch the login activity somehow
                          val intent = Intent(this, LoginActivity::class.java)
                          startActivity(intent)
                  }
  
                  selectphoto_button_register.setOnClickListener {
                          Log.d(&quot;RegisterActivity&quot;, &quot;Try to show photo selector&quot;)
  
                          val intent = Intent(Intent.ACTION_PICK)
                          intent.type = &quot;image/*&quot;
                          startActivityForResult(intent, 0)
                  }
          }
  
          var selectedPhotoUri: Uri? = null
  
          override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
                  super.onActivityResult(requestCode, resultCode, data)
  
                  if (requestCode == 0 &amp;&amp; resultCode == Activity.RESULT_OK &amp;&amp; data != null) {
                          Log.d(&quot;RegisterActivity&quot;, &quot;Photo was selected&quot;)
  
                          selectedPhotoUri = data.data
  
                          val bitmap = MediaStore.Images.Media.getBitmap(contentResolver,selectedPhotoUri)
  
                          select_photo_imageView_register.setImageBitmap(bitmap)
                          selectphoto_button_register.alpha = 0f
                  }
          }
  
          private fun performRegister() {
                  val username = username_edittext_register.text.toString()
                  val email = email_edittext_register.text.toString()
                  val password = password_edittext_register.text.toString()
  
                  if (email.isEmpty() || password.isEmpty()) {
                          Toast.makeText(this,&quot;Please enter text in email/password&quot;, Toast.LENGTH_SHORT).show()
                          return
                  }
  
                  Log.d(&quot;RegisterActivity&quot;, &quot;Username: $username&quot;)
                  Log.d(&quot;RegisterActivity&quot;, &quot;Email is: $email&quot;)
                  Log.d(&quot;RegisterActivity&quot;, &quot;Password: $password&quot;)
  
                  // 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(&quot;RegisterActivity&quot;, &quot;Successfully created user with uid: ${it.result.user!!.uid}&quot;)
                                  uploadImageToFirebaseStorage()
                          }
                          .addOnFailureListener{
                                  Log.d(&quot;RegisterActivity&quot;,&quot;Failed to create user: ${it.message}&quot;)
                                  Toast.makeText(this,&quot;Failed to create user: ${it.message}&quot;, Toast.LENGTH_SHORT).show()
                          }
                  }
  
          private fun uploadImageToFirebaseStorage() {
                  if (selectedPhotoUri == null) return
  
                  val filename = UUID.randomUUID().toString()
                  val ref = FirebaseStorage.getInstance().getReference(&quot;/images/$filename&quot;)
  
                  ref.putFile(selectedPhotoUri!!)
                          .addOnSuccessListener {
                                  Log.d(&quot;RegisterActivity&quot;, &quot;Successfully uploaded image: ${it.metadata?.path}&quot;)
  
                                  ref.downloadUrl.addOnSuccessListener {
                                          Log.d(&quot;RegisterActivity&quot;, &quot;File Location: $it&quot;)
  
                                          saveUserToFirebaseDatabase(it.toString())
                                  }
                          }
                          .addOnFailureListener{
                                  Log.d(&quot;RegisterActivity&quot;, &quot;Failed&quot;)
                                  Toast.makeText(this,&quot;Failed: ${it.message}&quot;, Toast.LENGTH_SHORT).show()
                          }
          }
  
          private fun saveUserToFirebaseDatabase(profileImageUrl: String) {
                  val uid = FirebaseAuth.getInstance().uid ?: &quot;&quot;
                  val ref = FirebaseDatabase.getInstance().getReference(&quot;/users/$uid&quot;)
  
                  val user = User(uid, username_edittext_register.text.toString(), profileImageUrl)
  
                  ref.setValue(user)
                          .addOnSuccessListener {
                                  Log.d(&quot;RegisterActivity&quot;, &quot;Finally we saved the user to Firebase Database&quot;)
  
                                  val intent = Intent(this,LatestMessagesActivity::class.java)
                                  intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TASK.or(Intent.FLAG_ACTIVITY_NEW_TASK)
                                  startActivity(intent)
                          }
                          .addOnFailureListener{
                                  Log.d(&quot;RegisterActivity&quot;, &quot;Failed to set value to database: ${it.message}&quot;)
                          }
                          Log.d(&quot;saveUserToDatabase&quot;,&quot;Task completed? User: $user&quot;)
          }
  }
  
  class User(val uid: String,val username: String,val profileImageUrl: String) {
          constructor() : this(&quot;&quot;, &quot;&quot;, &quot;&quot;)
  }
  </code></pre>
  <p>This is the output of &quot;user&quot;: letsbuildthatapp.com.User@41cb40</p>