【问题标题】:Why FirebaseUser photoUrl run app not show?为什么 FirebaseUser photoUrl 运行应用程序不显示?
【发布时间】:2020-05-06 09:17:34
【问题描述】:

请问为什么运行app,FirebaseUser的profilePhoto头像会消失?

但如果只是登录或注销,FirebaseUser 的 profilePhoto 头像可以保留状态。

如果我从移动存储中选择一张照片到头像,然后注销并登录,头像还是一样的。但是运行app,头像会消失,为什么?

class ProfileFragment : Fragment() {

    companion object {
        val TAG = ProfileFragment::class.java.simpleName
        val instance by lazy {
            ProfileFragment()
        }
        var user:FirebaseUser? = null
        private val REQUEST_CODE_CHOOSE_AVATAR: Int = 200
//        var userPhotoUrl:Uri? = null
    }

    private lateinit var viewModel: ProfileViewModel

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.profile_fragment, container, false)
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        retainInstance = true
        viewModel = ViewModelProviders.of(this).get(ProfileViewModel::class.java)
        // TODO: Use the ViewModel
        user = FirebaseAuth.getInstance().currentUser
//        userPhotoUrl = user?.photoUrl
        Picasso.get()
            .load(user?.photoUrl)
            .placeholder(R.mipmap.ic_launcher)
            .transform(CropCircleTransformation())
            .into(avatar_url)
        name.text = user?.displayName

        avatar_url.setOnClickListener {
            val intent = Intent().apply {
                type = "image/*"
                action = Intent.ACTION_GET_CONTENT
                action = Intent.ACTION_PICK
            }
            startActivityForResult(Intent.createChooser(intent,"Choose avatar"),REQUEST_CODE_CHOOSE_AVATAR)

        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == REQUEST_CODE_CHOOSE_AVATAR) {
            if (resultCode == RESULT_OK) {
                if (data != null) {
//                    val user = FirebaseAuth.getInstance().currentUser
                    val uri = data.data
                    val userProfileChangeRequest = UserProfileChangeRequest.Builder()
                        .setPhotoUri(uri)
                        .build()
                    user?.updateProfile(userProfileChangeRequest)
                        ?.addOnCompleteListener {
                            if (it.isSuccessful) {
                                Picasso.get()
                                    .load(user?.photoUrl)
                                    .transform(CropCircleTransformation())
                                    .into(avatar_url)

                                FirebaseFirestore.getInstance()
                                    .collection("uploadedImages")
                                    .whereEqualTo("uid",user?.uid)
                                    .addSnapshotListener { querySnapshot, firebaseFirestoreException ->
                                        if (querySnapshot != null && !querySnapshot.isEmpty) {
                                            for (doc in querySnapshot.documents) {
                                                Log.d(TAG, "uploadedImages's doc: ${doc.data}");
                                                doc.data?.set("avatarUrl", user?.photoUrl.toString())
                                            }
                                        } else {
                                            firebaseFirestoreException.toString()
                                        }
                                    }

                                FirebaseFirestore.getInstance()
                                    .collection("userData")
                                    .document(user!!.uid)
                                    .update("avatarUrl", user!!.photoUrl.toString())
                            }

                        }

                }
            }
        }
    }
}

登录注销,头像依然保留

为什么重新运行app头像会消失?

【问题讨论】:

  • 您的问题不清楚。请编辑。

标签: android kotlin google-cloud-firestore firebase-authentication picasso


【解决方案1】:

问题已经解决了。

  override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
            super.onActivityResult(requestCode, resultCode, data)
            if (requestCode == REQUEST_CODE_CHOOSE_AVATAR) {
                if (resultCode == RESULT_OK) {
                    if (data != null) {
    //                    val user = FirebaseAuth.getInstance().currentUser
                        val uri = data.data

                        val avatarRef = FirebaseStorage.getInstance().reference
                            .child("userAvatar")
                            .child(user!!.uid)
                            .child(uri.toString())

                        avatarRef.putFile(uri!!)
                            .continueWithTask {
                                if (!it.isSuccessful) {
                                    it.exception?.let {
                                        throw it
                                    }
                                }
                                avatarRef.downloadUrl
                            }.addOnCompleteListener {
                                if (it.isSuccessful) {
                                    val avatarDownloadUrl = it.result
                                    val userProfileChangeRequest = UserProfileChangeRequest.Builder()
                                        .setPhotoUri(avatarDownloadUrl)
                                        .build()

                                    user?.updateProfile(userProfileChangeRequest)
                                        ?.addOnCompleteListener { it1 ->
                                            if (it1.isSuccessful) {
                                                Picasso.get()
                                                    .load(user?.photoUrl)
                                                    .transform(CropCircleTransformation())
                                                    .into(avatar_url)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-03
    • 1970-01-01
    • 2010-12-28
    相关资源
    最近更新 更多