kotlin 将信息保存到Firebase数据库和存储

xyhw6mcr  于 2023-06-24  发布在  Kotlin
关注(0)|答案(1)|浏览(138)

我一直在努力,但有些事情是错的。因此,用户应该将用户名,全名和图像保存到Firebase中的实时数据库和存储中,但当我单击“保存”按钮时,实际上什么也没有发生。未显示吐司消息,未显示进度对话框,数据未转到Firebase。我不知道问题出在哪里。下面是我的代码:

data class User(var userName: String ?= null, var fullName: String ?= null)

下面是用户应该放置数据的Activity:

lass AccountSettingsActivity : AppCompatActivity() {
    private lateinit var binding: ActivityAccountSettingsBinding
    private lateinit var auth: FirebaseAuth
    private lateinit var firebaseUser: FirebaseUser
    private lateinit var databaseReference: DatabaseReference
    private lateinit var storageReference: StorageReference
    private lateinit var imageUri: Uri

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityAccountSettingsBinding.inflate(layoutInflater)
        setContentView(binding.root)

        auth = FirebaseAuth.getInstance()
        val uid = auth.currentUser?.uid
        databaseReference = FirebaseDatabase.getInstance().getReference("Users")
        val save = findViewById<Button>(R.id.save_btn)
        save.setOnClickListener {
            val userName = binding.usernameProfileFrag.text.toString()
            val fullName = binding.fullNameProfileFrag.text.toString()

            val user = User(userName, fullName)
            if (uid != null){
                databaseReference.child(uid).setValue(user).addOnCompleteListener {
                    if (it.isSuccessful){
                    uploadProfilePic()
                    }else{
                        Toast.makeText(this@AccountSettingsActivity, "Failed to update profile", Toast.LENGTH_SHORT).show()
                    }
                }
            }
        }

     
    }

    private fun uploadProfilePic() {
        val progressDialog = ProgressDialog(this@AccountSettingsActivity)
        progressDialog.setTitle("Uploading")
        progressDialog.setMessage("Please wait, this may take a while")
        progressDialog.setCanceledOnTouchOutside(false)
        imageUri = Uri.parse("android.resource://$packageName/${R.drawable.profile}")
        storageReference = FirebaseStorage.getInstance().getReference("Users/"+auth.currentUser?.uid)
        storageReference.putFile(imageUri).addOnSuccessListener {

            progressDialog.show()
             Toast.makeText(this@AccountSettingsActivity, "Successfully uploaded", Toast.LENGTH_SHORT).show()
        }.addOnFailureListener{
            progressDialog.dismiss()
            Toast.makeText(this@AccountSettingsActivity, "Failed to upload the image", Toast.LENGTH_SHORT).show()
        }
    }
}
qojgxg4l

qojgxg4l1#

因为uid为null。首先,您必须从Firebase启用auth / sing-in方法/ anonymous

if (auth.currentUser == null) {
        auth.signInAnonymously().addOnSuccessListener {
            //created uid and do something
        }.addOnFailureListener {
            //error
        }
    } else {
        //you have a uid ! do something
    }

一个建议,添加uid变量到你的用户数据类!

相关问题