I'm using Firebase Auth to manage a user's profile. The users are able to set avatar for their account using firebase auth's stock profile builder.
My code for setting avatar is
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode)
{
case CODE_PICK:
{
if(resultCode == Activity.RESULT_OK)
{
if(data != null)
{
loadingDialog.ShowLoadingDialog();
Uri uri = data.getData();
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
UserProfileChangeRequest profileUpdates = new UserProfileChangeRequest.Builder()
.setPhotoUri(uri)
.build();
if(user != null)
{
user.updateProfile(profileUpdates)
.addOnCompleteListener(new OnCompleteListener<Void>()
{
@Override
public void onComplete(@NonNull @NotNull Task<Void> task)
{
if(task.isSuccessful())
{
Target target = new PicassoImageTarget().picassoImageTarget(AccountActivity.this, "media", "avatar.png");
Picasso.get()
.load(uri)
.into(target);
userVars.setAvatar(true);
}
else
{
Toast.makeText(AccountActivity.this, "Failed to upload avatar! Please try again later.", Toast.LENGTH_LONG).show();
}
loadingDialog.HideLoadingDialog();
}
});
}
}
My code for retrieving the photo when a user logs in is
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user != null)
{
Uri photoUrl = user.getPhotoUrl();
Log.e("SSA", String.valueOf(photoUrl));
Target target = new PicassoImageTarget().picassoImageTarget(SplashScreenActivity.this, "media", "avatar.png");
if(photoUrl != null)
{
Picasso.get()
.load(photoUrl)
.into(target);
userVars.setAvatar(true);
}
else
{
Picasso.get()
.load(R.drawable.ic_main_avatar)
.into(target);
userVars.setAvatar(false);
}
}
When I log the photoUrl returned after the Uri photoUrl = user.getPhotoUrl();
it returns the local path (E/SSA: file:///storage/emulated/0/DCIM/Camera/imagename.jpg
) instead of the remote link.
Hence my avatar isn't loaded when opening the same account from a different device or in the same device after clearing data.
Firebase Authentication doesn't do any processing on the photo URL you provide. So when you call .setPhotoUri(uri)
, that uri
is a local image and that is literally the value it stores.
If you want a URL that others can also access, you'll first need to upload the image somewhere, get the URL to the image in that location, and then pass that URL to setPhotoUri()
.
A common place to store these images is in Cloud Storage for Firebase, although that is in no way required. If you'd like to do that, check out its documentation on uploading an image and then getting a download URL (which is a publicly readable, read-only URL that you probably want to use for the profile picture).