Firebase Google Sign In - Photo URI



我能够使用Firebase文档中的此代码获取照片URI

FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user != null) {
    for (UserInfo profile : user.getProviderData()) {
        // Id of the provider (ex: google.com)
        String providerId = profile.getProviderId();
        // UID specific to the provider
        String uid = profile.getUid();
        // Name, email address, and profile photo Url
        String name = profile.getDisplayName();
        String email = profile.getEmail();
        Uri photoUrl = profile.getPhotoUrl();
    };
}

我也有一个名为mPic的ImageView,我尝试了这段代码。

mPic.setImageURI(null);
mPic.setImageURI(photoUrl);

不幸的是,我无法查看照片。关于如何做到这一点的任何提示?

尝试使用毕加索图书馆。 http://square.github.io/picasso/

Picasso.with(context)
           .load(url)
           .placeholder(R.drawable.placeholder)
           .resize(imgWidth, imgHeight)
           .centerCrop()
           .into(image);
 Picasso.with(getApplicationContext())
                        .load(mAuth.getCurrentUser().getPhotoUrl())
                        .resize(50, 50)
                        .centerCrop()
                        .into(yourImageView);

我只是想从"photoUri"中检索图像以用于Jetpack Compose。经过无数次尝试,我发现URI实际上保存了照片的URL,我们只需要提取它。在我的应用程序中,我使用以下代码(Kotlin(提取了URL:

fun Uri?.getUrl(): String? = this?.run { "https://$host$path" }

获取 URL 后,您可以以任何方式自由使用它。在我的用例中,我将其与线圈一起使用。

     AsyncImage(
         model = 
               ImageRequest.Builder(LocalContext.current)
               .data(url) //Url retrieved from the above function
               .crossfade(true)
               .build(),
         contentDescription = "User image",
     )

注意:您也可以直接在数据[.data(photoUri(]中使用"photoUri"。

最新更新