我有一个屏幕,它的作用是用户可以从屏幕上显示的头像中选择一个头像作为他们的个人资料照片,然后我在个人资料片段中有一个屏幕,在其中显示用户名和个人资料照片,我使用 currentUser.photoUrl 作为个人资料照片,但当我运行应用程序时,它不会加载照片,也不会在 Firebase 中保存照片,我使用一种名为“saveAvatar”的方法来执行此操作:
暂停有趣的 saveAvatar(imageBitmap: 位图?) { val 用户 = FirebaseAuth.getInstance().currentUser
if (user != null && imageBitmap != null) {
val imageRef =
FirebaseStorage.getInstance().reference.child("${user.email}/profile_picture")
val baos = ByteArrayOutputStream()
imageBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos)
val data = baos.toByteArray()
// Sube la imagen a Firebase Storage
imageRef.putBytes(data).await()
// Obtiene la URL de descarga de la imagen cargada
val downloadUrl = imageRef.downloadUrl.await().toString()
// Actualiza la foto de perfil en el perfil del usuario
val profileUpdates = UserProfileChangeRequest.Builder()
.setPhotoUri(Uri.parse(downloadUrl))
.build()
user.updateProfile(profileUpdates)
}
}
在选择头像的片段中我有这个:
binding.imgProfile1.setOnClickListener {
selectedAvatar = BitmapFactory.decodeResource(resources, R.drawable.ricardobochini)
binding.imgProfile1.setImageBitmap(selectedAvatar)
}
binding.imgProfile2.setOnClickListener {
selectedAvatar = BitmapFactory.decodeResource(resources, R.drawable.ricardobochini)
binding.imgProfile2.setImageBitmap(selectedAvatar)
}
binding.imgProfile3.setOnClickListener {
selectedAvatar = BitmapFactory.decodeResource(resources, R.drawable.ricardobochini)
binding.imgProfile3.setImageBitmap(selectedAvatar)
}
binding.imgProfile4.setOnClickListener {
selectedAvatar = BitmapFactory.decodeResource(resources, R.drawable.ricardobochini)
binding.imgProfile4.setImageBitmap(selectedAvatar)
}
binding.imgProfile5.setOnClickListener {
selectedAvatar = BitmapFactory.decodeResource(resources, R.drawable.ricardobochini)
binding.imgProfile5.setImageBitmap(selectedAvatar)
}
binding.imgProfile6.setOnClickListener {
selectedAvatar = BitmapFactory.decodeResource(resources, R.drawable.ricardobochini)
binding.imgProfile6.setImageBitmap(selectedAvatar)
}
binding.selectImage.setOnClickListener {
if (selectedAvatar != null) {
viewModel.saveAvatar(selectedAvatar)
findNavController().navigate(R.id.action_avatarProfileFragment_to_homeFragment)
}
}
binding.skip.setOnClickListener {
findNavController().navigate(R.id.action_avatarProfileFragment_to_homeFragment)
}
为了在个人资料中显示照片,我使用这个:
Glide.with(this).load(currentUser?.photoUrl).centerCrop().into(binding.avatarProfile)
我认为我在 saveAvatar 中做错了什么,但我不知道是什么,我已经尝试了几种方法,但它无法显示照片
val user = Firebase.auth.currentUser
val profileUpdates = userProfileChangeRequest {
displayName = "Jane Q. User"
photoUri = Uri.parse(downloadUrl)
}
user!!.updateProfile(profileUpdates)
.addOnCompleteListener { task ->
if (task.isSuccessful) {
Log.d(TAG, "User profile updated.")
}
}
val user = Firebase.auth.currentUser
user?.let {
for (profile in it.providerData) {
// Id of the provider (ex: google.com)
val providerId = profile.providerId
// UID specific to the provider
val uid = profile.uid
// Name, email address, and profile photo Url
val name = profile.displayName
val email = profile.email
val photoUrl = profile.photoUrl
}
}
val user = Firebase.auth.currentUser
user?.let {
// Name, email address, and profile photo Url
val name = it.displayName
val email = it.email
val photoUrl = it.photoUrl
// Check if user's email is verified
val emailVerified = it.isEmailVerified
// The user's ID, unique to the Firebase project. Do NOT use this value to
// authenticate with your backend server, if you have one. Use
// FirebaseUser.getIdToken() instead.
val uid = it.uid
}