留言板

How to retrieve User profile picture

ronnie orvakanti,修改在12 年前。

How to retrieve User profile picture

Junior Member 帖子: 27 加入日期: 12-2-27 最近的帖子
Hi All,
How do i retrieve a Liferay user's profile picture in Java?

Thanks in advance emoticon
thumbnail
David H Nebinger,修改在12 年前。

RE: How to retrieve User profile picture

Liferay Legend 帖子: 14919 加入日期: 06-9-2 最近的帖子
Um, you look at any of the Liferay source code that does the same thing, then emulate it...
ronnie orvakanti,修改在12 年前。

RE: How to retrieve User profile picture

Junior Member 帖子: 27 加入日期: 12-2-27 最近的帖子
Hi David,
I looked. But couldn't find anything like for instance: user.getPortrait()
Im using Liferay 6.0.6.
thumbnail
David H Nebinger,修改在12 年前。

RE: How to retrieve User profile picture

Liferay Legend 帖子: 14919 加入日期: 06-9-2 最近的帖子
<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,修改在12 年前。

RE: How to retrieve User profile picture

Junior Member 帖子: 27 加入日期: 12-2-27 最近的帖子
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,修改在12 年前。

RE: How to retrieve User profile picture

Liferay Legend 帖子: 14919 加入日期: 06-9-2 最近的帖子
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,修改在12 年前。

RE: How to retrieve User profile picture

Junior Member 帖子: 27 加入日期: 12-2-27 最近的帖子
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,修改在12 年前。

RE: How to retrieve User profile picture

Liferay Legend 帖子: 14919 加入日期: 06-9-2 最近的帖子
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,修改在12 年前。

RE: How to retrieve User profile picture

Junior Member 帖子: 27 加入日期: 12-2-27 最近的帖子
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,修改在12 年前。

RE: How to retrieve User profile picture

Junior Member 帖子: 55 加入日期: 12-2-6 最近的帖子
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,修改在12 年前。

RE: How to retrieve User profile picture

Junior Member 帖子: 27 加入日期: 12-2-27 最近的帖子
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,修改在12 年前。

RE: How to retrieve User profile picture

Liferay Legend 帖子: 7942 加入日期: 10-3-24 最近的帖子
<%@ 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,修改在12 年前。

RE: How to retrieve User profile picture

Regular Member 帖子: 159 加入日期: 10-8-24 最近的帖子
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,修改在11 年前。

RE: How to retrieve User profile picture

Junior Member 帖子: 27 加入日期: 12-2-27 最近的帖子
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,修改在11 年前。

RE: How to retrieve User profile picture

Junior Member 帖子: 27 加入日期: 12-2-27 最近的帖子
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,修改在12 年前。

RE: How to retrieve User profile picture

Regular Member 帖子: 159 加入日期: 10-8-24 最近的帖子
For jsp code you can refer the code given by David.
thumbnail
Santosh B Biradar,修改在6 年前。

RE: How to retrieve User profile picture

Junior Member 帖子: 41 加入日期: 15-8-4 最近的帖子
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,修改在6 年前。

RE: How to retrieve User profile picture

Liferay Legend 帖子: 14919 加入日期: 06-9-2 最近的帖子
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,修改在6 年前。

RE: How to retrieve User profile picture

Junior Member 帖子: 41 加入日期: 15-8-4 最近的帖子
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,修改在6 年前。

RE: How to retrieve User profile picture

Liferay Legend 帖子: 14919 加入日期: 06-9-2 最近的帖子
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!