Updating user profile photo does not update it with currentUser.photoUrl

Viewed 598

I have take a photo, then I update my current user photo to apply this change to the Firebase authenticated user.

What I spect is after updating this photo to the authenticated user, to get the photoUrl as a string like when I first sign in in my app

suspend fun updatePhoto(uri: Uri): Resource<Unit>{
        val currentUser = FirebaseAuth.getInstance().currentUser
        val profileUpdates = UserProfileChangeRequest.Builder().setPhotoUri(uri).build()
        currentUser!!.updateProfile(profileUpdates).await()
        currentUser.reload().await()
        val userData = hashMapOf<String,Any>()
        userData["photoUrl"] = currentUser.photoUrl.toString()
        FirebaseFirestore
            .getInstance()
            .collection("user")
            .document(currentUser.uid).set(userData, SetOptions.merge()).await()
        return Resource.Success(Unit)
    }

At this line userData["photoUrl"] = currentUser.photoUrl.toString() I expect the actual photoUrl updated in firebase and I'm getting a local Uri like file://my.com.app/cache/photo.jpg

If I updated the user photo from the authentication itself, does not firebase handle the updated photo and needs to return me the actual photo updated by that current user ?

I want to avoid using firebase storage to upload the photo and place it in my database, instead, I want to retrieve it directly from the updated user photo

1 Answers

When you set a Firebase Auth user photo url, it does not fetch the contents of that image and store it. You probably passed that file URL from another part of your app, and now it's giving you back the same string, as expected.

I don't think there is any way to avoid uploading that image to a service that can host it for you, like Cloud Storage.

Related