TheLettuceMaster
TheLettuceMaster

Reputation: 15744

Getting complete URL of User's Google Profile Picture

I use Google sign-in in an Android app and save a link to their profile picture URL in the database.

In the past, I would get the complete URL, starting with this one of these, for example:

https://lh3.googleusercontent.com/{random stuff}/photo.jpg
https://lh4.googleusercontent.com/{random stuff}/photo.jpg
https://lh5.googleusercontent.com/{random stuff}/photo.jpg
https://lh6.googleusercontent.com/{random stuff}/photo.jpg

Ever since Google Play Services updated this (I think in 8.3?) I only get

/{random stuff}/photo.jpg

This obviously won't link to a picture.

Here is my code:

    GoogleSignInAccount acct = result.getSignInAccount();

    if (acct != null) 
    {
        String profilePicPath = "";
        if (acct.getPhotoUrl() != null) {
            profilePicPath = acct.getPhotoUrl().getPath();
        }
    }

What am I doing wrong?

EDIT: I believe what I am doing wrong is I added getPath() after the URL.

Upvotes: 1

Views: 2839

Answers (2)

TheLettuceMaster
TheLettuceMaster

Reputation: 15744

The problem was this:

I added getPath() here profilePicPath = acct.getPhotoUrl().getPath();. Instead, I removed that, got its string form, and that is all I needed.

Upvotes: 1

GvSharma
GvSharma

Reputation: 2670

You will need Enabling G+ API on google console.

enter link description here

You will need to initialize the GoogleApiClient

enter link description here

    @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

   ......
    googleApiClient = new GoogleApiClient.Builder(getActivity())
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(Plus.API)
            .addScope(Plus.SCOPE_PLUS_LOGIN)
            .addScope(Plus.SCOPE_PLUS_PROFILE)
            .build();
}
@Override
public void onConnected(Bundle bundle) {

    Plus.PeopleApi.loadVisible(googleApiClient, null).setResultCallback(this);



    if (Plus.PeopleApi.getCurrentPerson(googleApiClient) != null) {
        Person person = Plus.PeopleApi.getCurrentPerson(googleApiClient);
        personNameView.setText(person.getDisplayName());
        if (person.hasImage()) {

            Person.Image image = person.getImage();


            new AsyncTask<String, Void, Bitmap>() {

                @Override
                protected Bitmap doInBackground(String... params) {

                    try {
                        URL url = new URL(params[0]);
                        InputStream in = url.openStream();
                        return BitmapFactory.decodeStream(in);
                    } catch (Exception e) {
                    /* TODO log error */
                    }
                    return null;
                }

                @Override
                protected void onPostExecute(Bitmap bitmap) {
                    personImageView.setImageBitmap(bitmap);
                }
            }.execute(image.getUrl());
        }
   }

enter link description here

Hope this helps

Upvotes: 0

Related Questions