Foros de discusión

How to retrieve User profile picture

ronnie orvakanti, modificado hace 11 años.

How to retrieve User profile picture

Junior Member Mensajes: 27 Fecha de incorporación: 27/02/12 Mensajes recientes
Hi All,
How do i retrieve a Liferay user's profile picture in Java?

Thanks in advance emoticon
thumbnail
David H Nebinger, modificado hace 11 años.

RE: How to retrieve User profile picture

Liferay Legend Mensajes: 14914 Fecha de incorporación: 2/09/06 Mensajes recientes
Um, you look at any of the Liferay source code that does the same thing, then emulate it...
ronnie orvakanti, modificado hace 11 años.

RE: How to retrieve User profile picture

Junior Member Mensajes: 27 Fecha de incorporación: 27/02/12 Mensajes recientes
Hi David,
I looked. But couldn't find anything like for instance: user.getPortrait()
Im using Liferay 6.0.6.
thumbnail
David H Nebinger, modificado hace 11 años.

RE: How to retrieve User profile picture

Liferay Legend Mensajes: 14914 Fecha de incorporación: 2/09/06 Mensajes recientes
<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, modificado hace 11 años.

RE: How to retrieve User profile picture

Junior Member Mensajes: 27 Fecha de incorporación: 27/02/12 Mensajes recientes
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, modificado hace 11 años.

RE: How to retrieve User profile picture

Liferay Legend Mensajes: 14914 Fecha de incorporación: 2/09/06 Mensajes recientes
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, modificado hace 11 años.

RE: How to retrieve User profile picture

Junior Member Mensajes: 27 Fecha de incorporación: 27/02/12 Mensajes recientes
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, modificado hace 11 años.

RE: How to retrieve User profile picture

Liferay Legend Mensajes: 14914 Fecha de incorporación: 2/09/06 Mensajes recientes
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, modificado hace 11 años.

RE: How to retrieve User profile picture

Junior Member Mensajes: 27 Fecha de incorporación: 27/02/12 Mensajes recientes
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, modificado hace 11 años.

RE: How to retrieve User profile picture

Junior Member Mensajes: 55 Fecha de incorporación: 6/02/12 Mensajes recientes
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, modificado hace 11 años.

RE: How to retrieve User profile picture

Junior Member Mensajes: 27 Fecha de incorporación: 27/02/12 Mensajes recientes
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, modificado hace 11 años.

RE: How to retrieve User profile picture

Liferay Legend Mensajes: 7942 Fecha de incorporación: 24/03/10 Mensajes recientes
<%@ 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, modificado hace 11 años.

RE: How to retrieve User profile picture

Regular Member Mensajes: 159 Fecha de incorporación: 24/08/10 Mensajes recientes
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, modificado hace 11 años.

RE: How to retrieve User profile picture

Junior Member Mensajes: 27 Fecha de incorporación: 27/02/12 Mensajes recientes
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, modificado hace 11 años.

RE: How to retrieve User profile picture

Junior Member Mensajes: 27 Fecha de incorporación: 27/02/12 Mensajes recientes
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, modificado hace 11 años.

RE: How to retrieve User profile picture

Regular Member Mensajes: 159 Fecha de incorporación: 24/08/10 Mensajes recientes
For jsp code you can refer the code given by David.
thumbnail
Santosh B Biradar, modificado hace 6 años.

RE: How to retrieve User profile picture

Junior Member Mensajes: 41 Fecha de incorporación: 4/08/15 Mensajes recientes
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, modificado hace 6 años.

RE: How to retrieve User profile picture

Liferay Legend Mensajes: 14914 Fecha de incorporación: 2/09/06 Mensajes recientes
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, modificado hace 6 años.

RE: How to retrieve User profile picture

Junior Member Mensajes: 41 Fecha de incorporación: 4/08/15 Mensajes recientes
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, modificado hace 6 años.

RE: How to retrieve User profile picture

Liferay Legend Mensajes: 14914 Fecha de incorporación: 2/09/06 Mensajes recientes
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!