【发布时间】: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())
}
}
}
}
}
}
}
登录注销,头像依然保留
【问题讨论】:
-
您的问题不清楚。请编辑。
标签: android kotlin google-cloud-firestore firebase-authentication picasso