Displaying Google Plus user profile picture in Android app - android

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.

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/

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);
}
}

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);
}

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()

Related

getPhotoUrl() or profile pic with google login

I'm using Google Api to integrate google login in my Android App.
It works very well, at the moment, and I'm able to retrieve the user name and the email, but I'm not able to retrieve the user profile pic.
When I try to execute account.getPhotoUrl();, I always get "null".
I set the profile picture in my google account, so I'm pretty sure I should get something different from "null".
Obviously, photo_url is declared as Uri.
As I read on Google Developers, I added .requestProfile() to retrieve information about the user profile, but I always get null for the profile photo.
How can I solve the problem?
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.requestProfile()
.build();
mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
....
private void handleSignInResult(Task<GoogleSignInAccount> completedTask) {
try {
GoogleSignInAccount account = completedTask.getResult(ApiException.class);
googleName = account.getDisplayName();
goofleEmail = account.getEmail();
photo_url = account.getPhotoUrl();
Log.w(TAG, "test url" + photo_url);
mainPresenter.dataSocialAPI(googleName, goofleEmail, userType);
signOut();
} catch (ApiException e) {
Log.w(TAG, "signInResult:failed code=" + e.getStatusCode());
}
}
Might be late but i found this solution with Picasso:
Add this in your app/build.grade under dependencies
implementation 'com.squareup.picasso:picasso:2.5.2'
In your Activity:
Uri personPhoto = acct.getPhotoUrl();
photo = root.findViewById(R.id.accPhoto);
Picasso.with(this).load(personPhoto).into(photo);

Android Studio: Facebook image won't load

I might me doing something wrong. I managed to get the facebook image with user.getPhotoUrl but it only shows a low res picture.
So I tried using this code.
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user != null) {
String name = user.getDisplayName();
nameView.setText(user.getDisplayName());
Picasso.with(this).load("https://graph.facebook.com/"+user.getUid()+"/picture?type=large").into(profileImage);
}
What am I doing wrong here?
Use this link to get a picture of a quality that you want. Just replace width=200 with the width you want and height=200 with the height you want.
"https://graph.facebook.com/"+user.getUid()+"/picture?type=large&width=200&height=200"
I managed to do it!
for(UserInfo profile : user.getProviderData()) {
if(profile.getProviderId().equals(getString("facebook.com"))) {
facebookUserId = profile.getUid();
}
}
String photoUrl = "https://graph.facebook.com/" + facebookUserId + "/picture?height=500";
Picasso.with(this).load(photoUrl).into(profileImage);
This worked for me

User info not loading into firebase auth

I am trying to create a very simple login with Facebook using firebase and android studio. My login with Facebook works and I was able to run the app and sign in but none of my info has been stored in firebase (I want to have the persons name, email, etc.) I know it's something small I am probably missing but I cannot figure it out and I have tried so many things. Also I checked and all my gradle files are up to date and my firebase is set up correctly so it has nothing to do with that. plz help.
firebaseAuth = FirebaseAuth.getInstance();
firebaseAuthListner = new FirebaseAuth.AuthStateListener(){
#Override
public void onAuthStateChanged(#NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = firebaseAuth.getCurrentUser();
if(user != null){
// what do i put here to pull out the fb users info into firebase?!
goMainScreen();
}
}
};
I have tried:
if(user != null){
String name = user.getDisplayName();
String email = user.getEmail();
String uid = user.getUid();
I have tried:
if (user != null) {
// User is signed in
Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid());
} else {
// User is signed out
Log.d(TAG, "onAuthStateChanged:signed_out");
}
I know it is probably a dumb mistake because I am new to firebase and android studio but any advice will help. Thank you
Are your user stored into the firebase console? (https://console.firebase.google.com)
If it is not stored in your project, it will not return anything when you call the getDisplayName(), getUser(), etc.
If they are stored, please post the complete code that you are using to do the login.

Android : How to get larger profile pic from Facebook using FirebaseAuth?

I am using FirebaseAuth to login user through FB. Here is the code:
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
private CallbackManager mCallbackManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getApplicationContext());
// Initialize Firebase Auth
mAuth = FirebaseAuth.getInstance();
mAuthListener = firebaseAuth -> {
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null) {
// User is signed in
Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid());
} else {
// User is signed out
Log.d(TAG, "onAuthStateChanged:signed_out");
}
if (user != null) {
Log.d(TAG, "User details : " + user.getDisplayName() + user.getEmail() + "\n" + user.getPhotoUrl() + "\n"
+ user.getUid() + "\n" + user.getToken(true) + "\n" + user.getProviderId());
}
};
}
The issue is that the photo in I get from using user.getPhotoUrl() is very small. I need a larger image and can't find a way to do that. Any help would be highly appreciated.
I have already tried this
Get larger facebook image through firebase login
but it's not working although they are for swift I don't think the API should differ.
It is not possible to obtain a profile picture from Firebase that is larger than the one provided by getPhotoUrl(). However, the Facebook graph makes it pretty simple to get a user's profile picture in any size you want, as long as you have the user's Facebook ID.
String facebookUserId = "";
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
ImageView profilePicture = (ImageView) findViewById(R.id.image_profile_picture);
// find the Facebook profile and get the user's id
for(UserInfo profile : user.getProviderData()) {
// check if the provider id matches "facebook.com"
if(FacebookAuthProvider.PROVIDER_ID.equals(profile.getProviderId())) {
facebookUserId = profile.getUid();
}
}
// construct the URL to the profile picture, with a custom height
// alternatively, use '?type=small|medium|large' instead of ?height=
String photoUrl = "https://graph.facebook.com/" + facebookUserId + "/picture?height=500";
// (optional) use Picasso to download and show to image
Picasso.with(this).load(photoUrl).into(profilePicture);
Two lines of code. FirebaseUser user = firebaseAuth.getCurrentUser();
String photoUrl = user.getPhotoUrl().toString();
photoUrl = photoUrl + "?height=500";
simply append "?height=500" at the end
If someone is looking for this but for Google account using FirebaseAuth. I have found a workaround for this. If you detail the picture URL:
https://lh4.googleusercontent.com/../.../.../.../s96-c/photo.jpg
The /s96-c/ specifies the image size (96x96 in this case)so you just need to replace that value with the desired size.
String url= FirebaseAuth.getInstance().getCurrentUser().getPhotoUrl();
url = url.replace("/s96-c/","/s300-c/");
You can analyze your photo URL to see if there is any other way to change its size.
As I said in the begining, this only works for Google accounts. Check #Mathias Brandt 's answer to get a custom facebook profile picture size.
EDIT 2020:
Thanks to Andres SK and #alextouzel for pointing this out. Photo URLs format have changed and now you can pass URL params to get different sizes of the picture. Check https://developers.google.com/people/image-sizing.
photoUrl = "https://graph.facebook.com/" + facebookId+ "/picture?height=500"
You can store this link to firebase database with user facebookId and use this in app.
Also you can change height as a parameter
Not for Android, but for iOS, but I thought it could be helpful for other people (I didn't find a iOS version of this question).
Based the provided answers I created a Swift 4.0 extension that adds a function urlForProfileImageFor(imageResolution:) to the Firebase User object. You can either ask for the standard thumbnail, a high resolution (I put this to 1024px but easily changed) or a custom resolution image. Enjoy:
extension User {
enum LoginType {
case anonymous
case email
case facebook
case google
case unknown
}
var loginType: LoginType {
if isAnonymous { return .anonymous }
for userInfo in providerData {
switch userInfo.providerID {
case FacebookAuthProviderID: return .facebook
case GoogleAuthProviderID : return .google
case EmailAuthProviderID : return .email
default : break
}
}
return .unknown
}
enum ImageResolution {
case thumbnail
case highres
case custom(size: UInt)
}
var facebookUserId : String? {
for userInfo in providerData {
switch userInfo.providerID {
case FacebookAuthProviderID: return userInfo.uid
default : break
}
}
return nil
}
func urlForProfileImageFor(imageResolution: ImageResolution) -> URL? {
switch imageResolution {
//for thumnail we just return the std photoUrl
case .thumbnail : return photoURL
//for high res we use a hardcoded value of 1024 pixels
case .highres : return urlForProfileImageFor(imageResolution:.custom(size: 1024))
//custom size is where the user specified its own value
case .custom(let size) :
switch loginType {
//for facebook we assemble the photoUrl based on the facebookUserId via the graph API
case .facebook :
guard let facebookUserId = facebookUserId else { return photoURL }
return URL(string: "https://graph.facebook.com/\(facebookUserId)/picture?height=\(size)")
//for google the trick is to replace the s96-c with our own requested size...
case .google :
guard var url = photoURL?.absoluteString else { return photoURL }
url = url.replacingOccurrences(of: "/s96-c/", with: "/s\(size)-c/")
return URL(string:url)
//all other providers we do not support anything special (yet) so return the standard photoURL
default : return photoURL
}
}
}
}
Note: From Graph API v8.0 you must provide the access token for every UserID request you do.
Hitting the graph API:
https://graph.facebook.com/<user_id>/picture?height=1000&access_token=<any_of_above_token>
With firebase:
FirebaseUser user = mAuth.getCurrentUser();
String photoUrl = user.getPhotoUrl() + "/picture?height=1000&access_token=" +
loginResult.getAccessToken().getToken();
You get the token from registerCallback just like this
LoginManager.getInstance().registerCallback(mCallbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
FirebaseUser user = mAuth.getCurrentUser();
String photoUrl = user.getPhotoUrl() + "/picture?height=1000&access_token=" + loginResult.getAccessToken().getToken();
}
#Override
public void onCancel() {
Log.d("Fb on Login", "facebook:onCancel");
}
#Override
public void onError(FacebookException error) {
Log.e("Fb on Login", "facebook:onError", error);
}
});
This is what documentation says:
Beginning October 24, 2020, an access token will be required for all
UID-based queries. If you query a UID and thus must include a token:
use a User access token for Facebook Login authenticated requests
use a Page access token for page-scoped requests
use an App access token for server-side requests
use a Client access token for mobile or web client-side requests
We recommend that you only use a Client token if you are unable to use
one of the other token types.
I use this code in a Second Activity, after having already logged in, for me the Token that is obtained in loginResult.getAccessToken().getToken(); It expires after a while, so researching I found this and it has served me
final String img = mAuthProvider.imgUsuario().toString(); // is = mAuth.getCurrentUser().getPhotoUrl().toString;
final String newToken = "?height=1000&access_token=" + AccessToken.getCurrentAccessToken().getToken();
Picasso.get().load(img + newToken).into("Image reference");
Check below response
final graphResponse = await http.get(
'https://graph.facebook.com/v2.12/me?fields=name,picture.width(800).height(800),first_name,last_name,email&access_token=${fbToken}');

GoogleSignInAccount getPhotoUrl() return null

Trying to get photo from signed in profile. But always return null. Name and email return values, trouble only with photo.
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestProfile()
.requestEmail()
.build();
mGoogleApiClient = new GoogleApiClient.Builder(StartActivity.this)
.enableAutoManage(StartActivity.this, StartActivity.this)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build();
acct = gResult.getSignInAccount();
String name = acct.getDisplayName(); //okay, value != null
String email = acct.getEmail(); //okay, value != null
Uri photoUri = acct.getPhotoUrl() //not okay, value == null
Why does it happen so? Account signed, email and name got, but photo always fail.
According to Google's documentation - GoogleSignInAccount
public Uri getPhotoUrl ()
Gets the photo url of the signed in user.
Returns
photo url for the Google account. Only non-null if requestProfile() is configured and user does have a Google+ profile
picture.
Please check if your Google account has had Google+ profile picture or not.
P/S: sometimes, if Google+ profile picture has been created already but after the time you add Google account in your device, perhaps you need to delete that existing Google account from your device, then re-add.
Was facing the same problem that is getting photoURL and
tokenID as null
As #BNK had already explained what can be the other cause(s)
therefore just adding my solution into it, for someone who is facing the same problem (hoping, this help them)
While using below code (written in kotlin)
val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.build()
I am just able to fetch information like
'DisplayName','Email','FamilyName','ID' except photoUrl and tokenId
So I just added 'requestIdToken' to GSO Builder like this:
val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.requestIdToken(resources.getString(R.string.googleAccountWebClientID)) // This line did the magic for me
.build()
As a result of this I am able fetch all the information along with
'idToken' and 'photoUrl'
Note: Kindly use 'WebClientId' for requestIdToken() which you can get from (https://console.developers.google.com/apis/credentials?project=)
if acct.getPhotoUrl() return null
means your google account is not associated with Google Plus.. This will not give you null if you have a proper google plus account
Solution:
There will be a chance to get null ...Solution is given below
there is one rest webservice by which you will get your imageurl
https://www.googleapis.com/plus/v1/people/{id}?fields=image&key={API_KEY}
"id" you will get by acct.getId() method calling and API_KEY you will get from your developer console...make sure that API_KEY is Browser key....not android or server key....
Enable Google+ Plus api there itself in developer console...
Now you can get your profile image via call above service..
https://www.googleapis.com/plus/v1/people/{id}?fields=image&key={API_KEY}
you will get a response like below
{
"image": {
"url": "https://lh4.googleusercontent.com/-StnGV_eLi3s/AAAAAAAAAAI/AAAAAAAABHU/XLH5wQ_Rm9E/photo.jpg?sz=50",
"isDefault": false
}
}
Now you got your url as imageUrl....
Now Smile Please and keep coding!!!
Try this code I this its work for your problem.
String _name;
#Override
public void onConnected(Bundle arg0) {
Log.e(TAG, "onConnected");
Plus.PeopleApi.loadVisible(mGoogleApiClient, null).setResultCallback(this);
Person currentUser = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient);
Log.e("USERNAME_Con",Plus.AccountApi.getAccountName(mGoogleApiClient));
Log.e("USERNAME_Con",currentUser.getBirthday()+" "+currentUser.getImage().getUrl()+" "+currentUser.getId());
String personPhotoUrl=currentUser.getImage().getUrl();
if(currentUser!=null){
String email=Plus.AccountApi.getAccountName(mGoogleApiClient);
String id=currentUser.getId();
_name=currentUser.getDisplayName();
progressDialog = ProgressDialog.show(Form.this, "", "Loading...", true,false);
logingoogle(email,id,_name);
}
personPhotoUrl = personPhotoUrl.substring(0,
personPhotoUrl.length() - 2)
+ 400;
Log.e("USER Image",""+personPhotoUrl);
new LoadProfileImage().execute(personPhotoUrl);
// Indicate that the sign in process is complete.
mSignInProgress = STATE_DEFAULT;
}
private class LoadProfileImage extends AsyncTask<String, Void, Bitmap> {
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
InputStream in = new URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
camera=mIcon11;
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
// bmImage.setImageBitmap(result);
setimage(camera);
}
}
if(acct.getPhotoUrl() == null){
//set default image
} else {
photo_url = acct.getPhotoUrl().toString(); //photo_url is String
}
If possible only you would compile with this version play services in your build.gradle change your old implementation with this:
implementation 'com.google.android.gms:play-services-auth:12.0.1'
after that you can do this action into activity result:
#Override protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RC_SIGN_IN) {
GoogleSignInResult result =Auth.GoogleSignInApi.getSignInResultFromIntent(data);
handleSignInResult(result);
}
}
and below:
private void handleSignInResult(GoogleSignInResult result) {
GoogleSignInAccount acct = result.getSignInAccount();
email = acct.getEmail();
first_name = acct.getDisplayName();
pic_profile = acct.getPhotoUrl().toString();
}
Works for me!!
When this happened to me in my android app I deleted the account off the device and put it back; and it worked. Originally I had no photo in the account. Then I added one. It was a device running Jellybean.

Categories

Resources