Ojonugwa Jude Ochalifu
Ojonugwa Jude Ochalifu

Reputation: 27237

Displaying Google Plus user profile picture in Android app

Am trying to display the profile picture of users logged into my app through Google+ but am not sure how to do this.To get the image (and other information), google provides the code

@Override
public void onConnected() {
...
if (mPlusClient.getCurrentPerson() != null) {
    Person currentPerson = mPlusClient.getCurrentPerson();
    String personName = currentPerson.getDisplayName();
    String personPhoto = currentPerson.getImage();
    String personGooglePlusProfile = currentPerson.getUrl();
}
}

I am aware that ordinarily i would need to get any image i want to display from res/drawable... but i don't know what to do with the value of personPhoto (which somehow get's changed from type String to Image when you paste the code in Eclipse.

Upvotes: 4

Views: 20314

Answers (4)

bensalcie
bensalcie

Reputation: 831

With new google sign in Options in Kotlin. Simply request for the parameters you need in your onCreate function

   val gso = 
     GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
        .requestIdToken(getString("your-client-id"))
        .requestEmail()
        .requestProfile()
        .requestId()
        .build()

When recieving back the response from google Sign in activity, inside the

Get user profile picture through:
    val credential = GoogleAuthProvider.getCredential(account?.idToken, null)

val photoUrl = credential.photoUrl.toString()

Upvotes: 0

Kenneth Murerwa
Kenneth Murerwa

Reputation: 888

This is the solution that worked on my Android app. I developed it from an answer above by Naveen Kumar Yadav.

Prerequisites - I am using a Google Sign-In API for my login. - I have an XML Code with an ImageView that has an id "client_dp"

Add this dependency to your app-level build.gradle file

dependencies {compile 'com.github.bumptech.glide:glide:3.8.0'}

Then add this code to your activity java file

//Firebase get user info
firebaseAuth = FirebaseAuth.getInstance();
FirebaseUser account = firebaseAuth.getCurrentUser();
if (account != null){         
        //Display User Image from Google Account
        //Objects.requireNonNull() prevents getPhotoUrl() from returning a NullPointerException
        String personImage = Objects.requireNonNull(account.getPhotoUrl()).toString();
        ImageView userImage = findViewById(R.id.client_dp);
        Glide.with(this).load(personImage).into(userImage);
    }

Upvotes: 0

Naveen Kumar Yadav
Naveen Kumar Yadav

Reputation: 151

First you need to add this dependency in your app/build.gradle file:

dependencies {compile 'com.github.bumptech.glide:glide:3.8.0'}

After this update your UI Accordingly :

private void updateUI(GoogleSignInAccount account) {
    if (account != null){
        text.setText("Sign in as :" +account.getDisplayName());
        email.setText(account.getEmail());
        String imgurl = account.getPhotoUrl().toString();
        Glide.with(this).load(imgurl).into(profile);
        sighIn.setVisibility(View.GONE);
        sighOut.setVisibility(View.VISIBLE);
    }
    else {
        text.setText("signOut");
        sighIn.setVisibility(View.VISIBLE);
        sighOut.setVisibility(View.GONE);
    }
}

Upvotes: 5

Nicholas Pesa
Nicholas Pesa

Reputation: 2196

You need to use that URL to grab the photo as a bitmap and set it to an imageview.

Section 4.9 of this article explains how to make an asynctask that will do just that:

http://www.androidhive.info/2014/02/android-login-with-google-plus-account-1/

Upvotes: 5

Related Questions