如何在firebase中保存可绘制的图像位图?

u3r8eeie  于 2023-11-21  发布在  其他
关注(0)|答案(1)|浏览(175)

我有一个屏幕,它所做的是,用户可以选择一个头像从那些出现在屏幕上是他们的个人资料照片,然后我在个人资料片段一个屏幕,我显示用户名和个人资料照片,我使用currentUser.photoUrl的个人资料照片,但当我运行应用程序它不加载照片,也不保存照片在Firebase,我使用了一个名为“saveAvatar”的方法来实现:
挂起保存Avatar(imageBitmap:Bitmap?){瓦尔用户= 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)
我想我在保存头像中做错了什么,但我不知道是什么,我已经尝试了几种方法,它不能显示照片

gg58donl

gg58donl1#

更新用户的配置文件

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
}

相关问题