Here is my code to display profile picture using gravatar.
ParseUser user = mUsers.get(position);
String email = user.getEmail().toLowerCase();
if (email.equals("")) {
holder.userImageView.setImageResource(R.drawable.avatar_empty);
} else {
String hash = MD5Util.md5Hex(email);
String gravatarUrl = "http://www.gravatar.com/avatar/" + hash + "?s=204&d=404";
Picasso.with(mContext).load(gravatarUrl)
.placeholder(R.drawable.avatar_empty)
.into(holder.userImageView);
}
But it is not showing even my profile picture and loads empty drawable.
You used a bunch of fake emails to populate your friend list, if so, gravatar won't be able to find images for those fake emails. So gravatar will return a 404, and your code will use the default "avatar_empty" image in the drawable folder.
A quick way to test to see if gravatar is working is to change the &d=404 to &d=monsterid and create a friend with a fake email "SDFSDSF#DSFSsdfsFSFSssf.com"
That friend will have an gravatar cartoon image.
Also you can try put http://www.gravatar.com/avatar/edb1260aa6f7f77688deee83e0a088f7?s=204&d=monsterid in your gravatarUrl. It will show picture.
Hope this helps someone , this is the code i used.
get library from:- https://github.com/tkeunebr/gravatar-android (you will get the .jar,, add it to lib)
Code for loading gravatar:
String gravatarUrl = Gravatar.init().with(user.email).size(100).build();
Picasso.with(mContext)
.load(gravatarUrl)
.into((ImageView) convertView.findViewById(R.id.user_avatar));
And if you want to set a default image in case of there is no gravatar set for the email(to override the default gravatar) then use:
String gravatarUrl = Gravatar.init().with(user.email).defaultImage(defaultImageUrl).size(100).build();
Hope this helps someone.
Related
The scenario:
I am building an app that allows the user to sign in using their Google account, it's worth noting that this is done using Firebase authentication.
The problem:
The user signs in perfectly but whenever i get their profile picture it is returned fine but it's very blurry.
I've researched and found some sites mentioning to change the resolution of the Google profile image but I'm not sure if that's possible. I know Facebook has a graph that allows for you to manipulate the URL of the profile image to change the resolution but I'm not sure how to improve the resolution of the profile image via Google.
I've posted a screenshot of the blurry image below:
This is the XML code for the ImageView that I'm loading the profile image in:
<de.hdodenhof.circleimageview.CircleImageView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/image_view_profile_pic"
android:layout_width="96dp"
android:layout_height="96dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="36dp"
android:src="#drawable/app_bar_tool_bar_bg"
app:civ_border_width="2dp"
app:civ_border_color="#FFFFFFFF"
android:contentDescription="#string/content_description_profile_picture" />
This is the code I used to get the profile image from Google via Firebase:
private void getUserProfileContent() {
// Check if the user is signed in
if (mUser != null) {
for (UserInfo profile : mUser.getProviderData()) {
// Profile photo url
Uri photoUrl = profile.getPhotoUrl();
// Load user's profile photo from their signed-in provider into the image view
Glide.with(ProfileActivity.this)
.load(photoUrl)
.into(mProfilePic);
}
}
}
This is the code to get the currently authenticated user:
// Variable declaration
private FirebaseUser mUser;
// Initializatioin in onCreate method
mUser = FirebaseAuth.getInstance().getCurrentUser();
Then of course, I call the above method in the onCreate method so it gets executed, like so:
getUserProfileContent();
I have read on this site that Google URLs can be manipulated to change the size of an image:
Details about Google's "magic" URL
Also, I've seen this site mentioning something similar about profile picture being blurry:
Firebase Google Group: Profile picture blurry
As it relates to the second link I haven't quite wrapped my head around how to carry out what they've suggested.
I solved my own problem, I will explain in full detail, see below.
Solution:
This is the newly modified code to my method in the question above, this is the fix:
private void getUserProfileContent() {
// Check if the user is signed in
if (mUser != null) {
for (UserInfo profile : mUser.getProviderData()) {
// Get the profile photo's url
Uri photoUrl = profile.getPhotoUrl();
// Variable holding the original String portion of the url that will be replaced
String originalPieceOfUrl = "s96-c/photo.jpg";
// Variable holding the new String portion of the url that does the replacing, to improve image quality
String newPieceOfUrlToAdd = "s400-c/photo.jpg";
// Check if the Url path is null
if (photoUrl != null) {
// Convert the Url to a String and store into a variable
String photoPath = photoUrl.toString();
// Replace the original part of the Url with the new part
String newString = photoPath.replace(originalPieceOfUrl, newPieceOfUrlToAdd);
// Load user's profile photo from their signed-in provider into the image view (with newly edited Url for resolution improvement)
Glide.with(ProfileActivity.this)
.load(newString)
.into(mProfilePic);
} // End if
} // End if
}
Explanation:
The code is self explanatory but for the sole purpose of clarity for many others who were having issues with this, I basically swapped out the part of the original Url that I retrieved from Google that specified the image's resolution with a higher resolution of my own.
Replaced this "s96-c/photo.jpg" with "s400-c/photo.jpg"
I have provided a screenshot below with TextViews displaying:
Url path of the original Url retrieved from Google (this one has the s96-c in its url)
Url path of the newly modified Url by me (this one has the s400-c in its url)
Original Url retrieved from Google with toString() method called on it. This is the third Url in the screenshot with s96-c
Newly modified Url by me with toString() method called on it. This is the fourth Url (similarly to the 3rd Url in the screenshot this Url has s96-c in its Url String)
In that order
Note:
I wanted to do an in depth explanation with this answer because I wanted to show that even though the string with low res was replaced with high res version, you'll notice the last TextView displays in its Url s96-c and the second-to-last Url (granted you're looking from top to bottom) displays the exact same thing, s96-c
BUT
The resolution indeed improved, so my theory is that even though the resolution improved, by me using a higher value, Google's system returned the improved image with a size of s96-c regardless (Let me know if anyone has a more concrete theory)
Screenshot of profile image with the blurriness fixed (improved resolution):
Anyway there you have it, I wanted to show a screenshot with the Urls and their differences so that I can explain how everything was happening, for anyone who was curious.
I hope this helps many others, because the blurry Google user profile image was a very inconvenient problem for myself.
In my case, inside of the HTML of my Ionic4 component I used:
<img class="avatar" [src]="user.photoURL + '?height=150'">
Adding the height parameter to the end of the URL worked for me.
Easy Fix
Just replace s96-c with s400-c or any other like s300-c
from the image url you get from google.
Example:
Here you can easily see the difference..
Look at the end of url.
https://lh4.googleusercontent.com/-C0Mqxb50Hxg/AAAAAAAAAAI/AAAAAAAADcw/xuG-pk0PzaI/s96-c/photo.jpg
https://lh4.googleusercontent.com/-C0Mqxb50Hxg/AAAAAAAAAAI/AAAAAAAADcw/xuG-pk0PzaI/s400-c/photo.jpg
I feel the code is much clearer this way
private void onSignedInInitialize(FirebaseUser user) {
if(user.getDisplayName() != null && user.getDisplayName().length() > 0)
{
usernameText.setText(user.getDisplayName());
}
String photoUrl;
String provider = user.getProviders().get(0);
if (provider.equals("facebook.com")) {
photoUrl = user.getPhotoUrl() + "?height=500";
}
else if(provider.equals("google.com"))
{
photoUrl = user.getPhotoUrl().toString();
//Remove thumbnail url and replace the original part of the Url with the new part
photoUrl = photoUrl.substring(0, photoUrl.length() - 15) + "s400-c/photo.jpg";
}
else
{
photoUrl = user.getPhotoUrl().toString();
}
Picasso.with(this)
.load(photoUrl)
.placeholder(R.drawable.profile_avatar)
.error(R.drawable.profile_avatar)
.into(circleImageView);
}
Instead of replacing the size, you can also just remove =s96-c from the photoUrl, and you will be receive the max size.
I am trying to get the image URL from the Dropbox and I followed the below code from the documentation.
try {
Entry directory = dropboxApi.metadata("/", 1000, null, true, null);
directory.mimeType = "image*//*";
for (Entry entry : directory.contents) {
DropboxAPI.DropboxLink link = dropboxApi.media(entry.path, false);
if (link.url.contains(".jpeg") || link.url.contains(".png") || link.url.contains(".jpg"))
files.add(link.url);
}
} catch (DropboxException e) {
e.printStackTrace();
}
Based on this code I can get the image URL. But it is not working after hours.
In my scenario, I want to get the image URL and I will send it to server API. So when I retrieve other API, I will get the image URL, what I sent. Once I got the URL, just I want to set it to imageview.
I have verified this link. I got the different link by using share method. But I don't know how to set that into my ImageView, whereas by using media method I can get the URL and I can set it to ImageView.
Please can someone help me to solve this problem
If i make an Http request for share url then I am getting image name with download option.
I am getting 16 headers.
From this am not getting any clue. There is no header name contains Location.There is one content-piracy-policy which gives some url values combined. But it is also not relevant.
The code below works to share an image. I'm having problems adding text to it without using a FB graph (which I dont want to do at this point). This code shares an image straight to Facebook but:
1) how do I also share text? I know how to do one or the other but cant seem to figure out both.
2) is it possible to have Facebookj scale the image?
Thank you for your help.
int imageToShare = getResources().getIdentifier(myCard.getImageId(),"drawable","com.gjnave.carddisplayertemplate");
Uri cardImageUri = Uri.parse("android.resource://com.gjnave.carddisplayertemplate/" + imageToShare);
bmpImage = MediaStore.Images.Media.getBitmap(this.getContentResolver(), cardImageUri);
performPublish(PendingAction.POST_PHOTO, false);
I used graph API Json Response of Facebook Wall Post Images and display in my APP i successfully got it. But the wall images look very Blur how to resolve? i used this code for get wall picture
URL url=new URL(hashMap.get("picture_url"));
bitmap=BitmapFactory.decodeStream(url.openConnection().getInputStream());
((ImageView)view.findViewById(R.id.imageView_FullImage)).setImageBitmap(bitmap)
The Facebook Graph API as well as the FQL data set always returns the Picture URL of a thumbnail. If you look at the URL it returns, it will have one of these ending (right before the image extension .jpg, .png, etc) _t., _a.. For example, if the URL is to a JPG file, it could have an ending _t.jpg
The idea is to swap the ending and choose a normal size for the image that is returned. To do this, use the code below that will replace the endings with the one for normal sized images (that should have the _n.)
By the way, I don't think the tag you are looking for is picture_url. It should be just picture. But regardless, get the source URL as shown below, replace the endings and then pass it to the this line in your code:
// THIS SHOULD BE AFTER THE if....else code block
bitmap=BitmapFactory.decodeStream(url.openConnection().getInputStream());
CODE TO REPLACE THE VARIOUS THUMBNAIL IMAGES: By the way, this is production code and works perfect.
String PICTURE_URL;
String getPicture = JOFeeds.getString("picture");
if (getPicture.contains("_t.")) {
PICTURE_URL = getPicture.replaceAll("_t.", "_n.");
} else if (getPicture.contains("_a.")) {
PICTURE_URL = getPicture.replaceAll("_a.", "_n.");
} else if (getPicture.contains("_s.")) {
PICTURE_URL = getPicture.replaceAll("_s.", "_n.");
} else if (getPicture.contains("_q.")) {
PICTURE_URL = getPicture.replaceAll("_q.", "_n.");
}
Note: However, in some cases, like a Video preview or a Link preview, it will not always have a bigger image available. Nothing much you can do about it nor can Facebook I suspect. These typically come from posts that are shared by users from other websites.
I'm trying to retrieve the currently-logged-in user's avatar image so that I can save it on my server and use it later in my application.
How would I go about getting the image directly?
Try this:
(replace USER_ID with alias or user ID...)
set source as https://graph.facebook.com/USER_ID/picture
for example large icon of a Page ID 149926629437 is:
http://graph.facebook.com/149926629437/picture?type=large
<img src="http://graph.facebook.com/149926629437/picture?type=large"/>
more info (specify size etc...):
http://developers.facebook.com/docs/reference/api/
part Pictures
(I also userd the aQuery (android query) to cache images...
aq.id( holder.image).image("https://graph.facebook.com/" + USER_ID + "/picture?type=normal");
http://code.google.com/p/android-query/
It seems working fine...
)
Can't say much about this, but you might want to try the facebook android sdk here ->https://github.com/facebook/facebook-android-sdk . Good luck :)
If you want a higher quality profile picture try:
https://graph.facebook.com/{ID}?fields=picture.height(2048)
And then retrieve the response.picture.data.url