掲示板

How to retrieve User profile picture

12年前 に ronnie orvakanti によって更新されました。

How to retrieve User profile picture

Junior Member 投稿: 27 参加年月日: 12/02/27 最新の投稿
Hi All,
How do i retrieve a Liferay user's profile picture in Java?

Thanks in advance emoticon
thumbnail
12年前 に David H Nebinger によって更新されました。

RE: How to retrieve User profile picture

Liferay Legend 投稿: 14919 参加年月日: 06/09/02 最新の投稿
Um, you look at any of the Liferay source code that does the same thing, then emulate it...
12年前 に ronnie orvakanti によって更新されました。

RE: How to retrieve User profile picture

Junior Member 投稿: 27 参加年月日: 12/02/27 最新の投稿
Hi David,
I looked. But couldn't find anything like for instance: user.getPortrait()
Im using Liferay 6.0.6.
thumbnail
12年前 に David H Nebinger によって更新されました。

RE: How to retrieve User profile picture

Liferay Legend 投稿: 14919 参加年月日: 06/09/02 最新の投稿
<portal-6.0.6-src>/portal-web/docroot/html/portlet/enterprise_admin/edit_user.jsp on line #223 says all it needs to.
12年前 に ronnie orvakanti によって更新されました。

RE: How to retrieve User profile picture

Junior Member 投稿: 27 参加年月日: 12/02/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
12年前 に David H Nebinger によって更新されました。

RE: How to retrieve User profile picture

Liferay Legend 投稿: 14919 参加年月日: 06/09/02 最新の投稿
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.
12年前 に ronnie orvakanti によって更新されました。

RE: How to retrieve User profile picture

Junior Member 投稿: 27 参加年月日: 12/02/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
12年前 に David H Nebinger によって更新されました。

RE: How to retrieve User profile picture

Liferay Legend 投稿: 14919 参加年月日: 06/09/02 最新の投稿
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.
12年前 に ronnie orvakanti によって更新されました。

RE: How to retrieve User profile picture

Junior Member 投稿: 27 参加年月日: 12/02/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
12年前 に Natalie D によって更新されました。

RE: How to retrieve User profile picture

Junior Member 投稿: 55 参加年月日: 12/02/06 最新の投稿
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
12年前 に ronnie orvakanti によって更新されました。

RE: How to retrieve User profile picture

Junior Member 投稿: 27 参加年月日: 12/02/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
12年前 に Hitoshi Ozawa によって更新されました。

RE: How to retrieve User profile picture

Liferay Legend 投稿: 7942 参加年月日: 10/03/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() %>" />
12年前 に Roshan Qureshi によって更新されました。

RE: How to retrieve User profile picture

Regular Member 投稿: 159 参加年月日: 10/08/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();
}
11年前 に ronnie orvakanti によって更新されました。

RE: How to retrieve User profile picture

Junior Member 投稿: 27 参加年月日: 12/02/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.
11年前 に ronnie orvakanti によって更新されました。

RE: How to retrieve User profile picture

Junior Member 投稿: 27 参加年月日: 12/02/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
12年前 に Roshan Qureshi によって更新されました。

RE: How to retrieve User profile picture

Regular Member 投稿: 159 参加年月日: 10/08/24 最新の投稿
For jsp code you can refer the code given by David.
thumbnail
6年前 に Santosh B Biradar によって更新されました。

RE: How to retrieve User profile picture

Junior Member 投稿: 41 参加年月日: 15/08/04 最新の投稿
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
6年前 に David H Nebinger によって更新されました。

RE: How to retrieve User profile picture

Liferay Legend 投稿: 14919 参加年月日: 06/09/02 最新の投稿
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
6年前 に Santosh B Biradar によって更新されました。

RE: How to retrieve User profile picture

Junior Member 投稿: 41 参加年月日: 15/08/04 最新の投稿
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
6年前 に David H Nebinger によって更新されました。

RE: How to retrieve User profile picture

Liferay Legend 投稿: 14919 参加年月日: 06/09/02 最新の投稿
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!