Forums de discussion

How to retrieve User profile picture

ronnie orvakanti, modifié il y a 11 années.

How to retrieve User profile picture

Junior Member Publications: 27 Date d'inscription: 27/02/12 Publications récentes
Hi All,
How do i retrieve a Liferay user's profile picture in Java?

Thanks in advance emoticon
thumbnail
David H Nebinger, modifié il y a 11 années.

RE: How to retrieve User profile picture

Liferay Legend Publications: 14914 Date d'inscription: 02/09/06 Publications récentes
Um, you look at any of the Liferay source code that does the same thing, then emulate it...
ronnie orvakanti, modifié il y a 11 années.

RE: How to retrieve User profile picture

Junior Member Publications: 27 Date d'inscription: 27/02/12 Publications récentes
Hi David,
I looked. But couldn't find anything like for instance: user.getPortrait()
Im using Liferay 6.0.6.
thumbnail
David H Nebinger, modifié il y a 11 années.

RE: How to retrieve User profile picture

Liferay Legend Publications: 14914 Date d'inscription: 02/09/06 Publications récentes
<portal-6.0.6-src>/portal-web/docroot/html/portlet/enterprise_admin/edit_user.jsp on line #223 says all it needs to.
ronnie orvakanti, modifié il y a 11 années.

RE: How to retrieve User profile picture

Junior Member Publications: 27 Date d'inscription: 27/02/12 Publications récentes
Hi David,
Is this what you are talking about?

<img alt="<%= HtmlUtil.escape(selUser.getFullName()) %>" class="avatar" src="<%= themeDisplay.getPathImage() %>/user_<%= selUser.isFemale() ? "female" : "male" %>_portrait?img_id=<%= selUser.getPortraitId() %>&t=<%= ImageServletTokenUtil.getToken(selUser.getPortraitId()) %>" />
thumbnail
David H Nebinger, modifié il y a 11 années.

RE: How to retrieve User profile picture

Liferay Legend Publications: 14914 Date d'inscription: 02/09/06 Publications récentes
No, that's for the generic portrait when the user doesn't have one loaded.

The line I'm looking at says <img alt="<%=HtmlUtil.escape(selUser.getFullName()) %>" class="user-logo" src="<%= selUser.getPortraitURL(themeDisplay) %>" />

Basically when you call user.getPortraitURL() with the current theme, you get the image link to use on the rendered page.

If you need direct access to the image file itself (which I doubt), you'd have to go through the image api to get to it.
ronnie orvakanti, modifié il y a 11 années.

RE: How to retrieve User profile picture

Junior Member Publications: 27 Date d'inscription: 27/02/12 Publications récentes
David,
I cant find that particular line in edit_user.jsp at all.

Im looking in the same folder you mentioned:
C:\workspace\liferay-portal-src-6.0.6\portal-web\docroot\html\portlet\enterprise_admin......

And im using Liferay 6.0.6
thumbnail
David H Nebinger, modifié il y a 11 années.

RE: How to retrieve User profile picture

Liferay Legend Publications: 14914 Date d'inscription: 02/09/06 Publications récentes
That line comes from 6.1 trunk, but a similar line is available in 6.0.6 also.

It's effectively the same thing - call user.getPortraitURL(themeDisplay) to get the URL for using as the src attribute of an image tag.
ronnie orvakanti, modifié il y a 11 années.

RE: How to retrieve User profile picture

Junior Member Publications: 27 Date d'inscription: 27/02/12 Publications récentes
Hi David,
Thank you for your response.

I retrieve a user (com.liferay.portal.model.user) from the DB using his userId. After that, i tried to get his portraitURL as you suggested:
user.getPortraitURL(themeDisplay)
But there is no such function call available.
I can only get his portraitID: user.getPortraitId();

I'm using Liferay 6.0.6. Is this API available only from Liferay 6.1?????
thumbnail
Natalie D, modifié il y a 11 années.

RE: How to retrieve User profile picture

Junior Member Publications: 55 Date d'inscription: 06/02/12 Publications récentes
David H Nebinger:
No, that's for the generic portrait when the user doesn't have one loaded.

The line I'm looking at says <img alt="<%=HtmlUtil.escape(selUser.getFullName()) %>" class="user-logo" src="<%= selUser.getPortraitURL(themeDisplay) %>" />

Basically when you call user.getPortraitURL() with the current theme, you get the image link to use on the rendered page.

If you need direct access to the image file itself (which I doubt), you'd have to go through the image api to get to it.



Above David's post contains all info you need.
I've used this way to retrieve user's portrait many times.

Natalie
ronnie orvakanti, modifié il y a 11 années.

RE: How to retrieve User profile picture

Junior Member Publications: 27 Date d'inscription: 27/02/12 Publications récentes
Hi Natalie,
How do i get my current theme display?

Could you please share a couple of code snippets where i could retrieve a user's theme display and then use it to retrieve the potraitURL?
thumbnail
Hitoshi Ozawa, modifié il y a 11 années.

RE: How to retrieve User profile picture

Liferay Legend Publications: 7942 Date d'inscription: 24/03/10 Publications récentes
<%@ taglib uri="http://java.sun.com/portlet" prefix="portlet" %>
<%@ taglib uri="http://liferay.com/tld/theme" prefix="liferay-theme" %>
<%@ taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui" %>
<liferay-theme:defineObjects />
<portlet:defineObjects />
<liferay-ui:user-display userId="<%= user.getUserId() %>" />
Roshan Qureshi, modifié il y a 11 années.

RE: How to retrieve User profile picture

Regular Member Publications: 159 Date d'inscription: 24/08/10 Publications récentes
In your portlet class you can use following code to retrieve user profile picture

ThemeDisplay themeDisplay= (ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
String portraitURL = null;
try {
portraitURL = themeDisplay.getUser().getPortraitURL(themeDisplay);
} catch (PortalException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (SystemException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
ronnie orvakanti, modifié il y a 11 années.

RE: How to retrieve User profile picture

Junior Member Publications: 27 Date d'inscription: 27/02/12 Publications récentes
Roshan Qureshi:
In your portlet class you can use following code to retrieve user profile picture

ThemeDisplay themeDisplay= (ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
String portraitURL = null;
try {
portraitURL = themeDisplay.getUser().getPortraitURL(themeDisplay);
} catch (PortalException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (SystemException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}



Hi Roshan,
I can only get the PortraitId but not the PortraitURL. I'm using LR 6.0.6 API. Which API are you talking about?

Thanks.
ronnie orvakanti, modifié il y a 11 années.

RE: How to retrieve User profile picture

Junior Member Publications: 27 Date d'inscription: 27/02/12 Publications récentes
Finally i was able to get the image object as follows:


long portraitId = user.getPortraitId();
        Image image = ImageLocalServiceUtil.getImage(portraitId);


This worked for me emoticon
Roshan Qureshi, modifié il y a 11 années.

RE: How to retrieve User profile picture

Regular Member Publications: 159 Date d'inscription: 24/08/10 Publications récentes
For jsp code you can refer the code given by David.
thumbnail
Santosh B Biradar, modifié il y a 6 années.

RE: How to retrieve User profile picture

Junior Member Publications: 41 Date d'inscription: 04/08/15 Publications récentes
How do i retrieve a Liferay user's profile picture in Java?

Hey ,

This code helps you,

user = UserLocalServiceUtil.getUser(userId);
long portraitId = user.getPortraitId();
String tokenId = com.liferay.portal.webserver.WebServerServletTokenUtil.getToken(user.getPortraitId());
profilePath = "/image/user_" + ((user != null) && user.isFemale() ? "female" : "male") + "_portrait?img_id=" + portraitId + "&t=" + tokenId;(URL is based on you requirement)

Thanks

Santosh B B
thumbnail
David H Nebinger, modifié il y a 6 années.

RE: How to retrieve User profile picture

Liferay Legend Publications: 14914 Date d'inscription: 02/09/06 Publications récentes
Santosh B Biradar:
This code helps you,

user = UserLocalServiceUtil.getUser(userId);
long portraitId = user.getPortraitId();
String tokenId = com.liferay.portal.webserver.WebServerServletTokenUtil.getToken(user.getPortraitId());
profilePath = "/image/user_" + ((user != null) && user.isFemale() ? "female" : "male") + "_portrait?img_id=" + portraitId + "&t=" + tokenId;(URL is based on you requirement)


Never, ever do hard-coded stuff like this...

What happens if someone changes the root for the portal? Everything else works but this code breaks. What happens when Liferay changes it in another version? Everything else works but this code breaks.

There are approved Liferay ways to access data via the APIs and there are hacks; this code is a hack that should be avoided.






Come meet me at the 2017 LSNA!
thumbnail
Santosh B Biradar, modifié il y a 6 années.

RE: How to retrieve User profile picture

Junior Member Publications: 41 Date d'inscription: 04/08/15 Publications récentes
Hi Devid ,

Actually I have used Liferay API's(like Dynamic query) to retrieve or access the user data and then only I have used above code to get user information.
Regards
Santosh B
thumbnail
David H Nebinger, modifié il y a 6 années.

RE: How to retrieve User profile picture

Liferay Legend Publications: 14914 Date d'inscription: 02/09/06 Publications récentes
But still, you shouldn't hard code strings like this.

IIRC the ThemeDisplay instance has a method to get the portrait URL, or the User object does taking the ThemeDisplay as an argument (I don't remember directly, but it is something along those lines).







Come meet me at Devcon 2017 or 2017 LSNA!