Foros de discusión

Android App getting user's avatar image (or message's attachemnts)

Zenobia L, modificado hace 10 años.

Android App getting user's avatar image (or message's attachemnts)

Junior Member Mensajes: 28 Fecha de incorporación: 8/04/11 Mensajes recientes
Hi,

is there a way for an android app (or any JSON client) to get from Liferay the user's avatar-image

how about the message-board's file attachments

my Android app is able to display the messages in a message board, including the information about the user who posted the message, but I don't find services to get the user's avatar image..

also, when attaching a image to a message (in the message board) in Liferay, this is nicely rendered in the message when displayed.. I can do that also in the android app but I cannot find a service to call to pull the image attached to the message

please let me know any feedback,
thank you
Zenobia L, modificado hace 10 años.

RE: Android App getting user's avatar image (or message's attachemnts)

Junior Member Mensajes: 28 Fecha de incorporación: 8/04/11 Mensajes recientes
I tried by retrieving a file from the document library
using JSON service /dlfileentry/get-file-as-stream
and when the file exists, it returns null

this is in 6.1 EE GA2

is there any way to get files from Liferay from remote clients using web services? (secure files)
thumbnail
Bruno Farache, modificado hace 10 años.

RE: Android App getting user's avatar image (or message's attachemnts)

Liferay Master Mensajes: 603 Fecha de incorporación: 14/05/07 Mensajes recientes
Hey Zenobia,

In the Mobile SDK, there's a PortraitUtil class, it can be used to download the user's portrait image.

As for downloading document library files, you can use WebDAV (each file display a WebDAV URL in the document library portlet). That would be a good thing to add to the SDK, I've created a ticket: MOBILESDK-10.
Zenobia L, modificado hace 10 años.

RE: Android App getting user's avatar image (or message's attachemnts)

Junior Member Mensajes: 28 Fecha de incorporación: 8/04/11 Mensajes recientes
thank you Bruno!

I am getting the avatar Images now, I did what the PortraitUtil class is doing to get these images, since they are public and do not need authentication, it worked

I am attaching some screenshots of my mobile app, it is for a class on developing mobile apps.. I am using Liferay as a server of social networks where people share posts about physical activity and nutrition, they can tag their posts so they can be searched later (eventually the location will be stored in Liferay) I am using the message boards as a repository of the posts-images-comments-tags.. and they are displayed in a different way in the Android app, each root message of a thread in a scrollable list, with the image attached just under the message (facebook style).. that is why I am accessing all these Liferay web-services using JSON...

I tried to access the images attached to a message board but there is no JSON web service to get those images and are not stored in the document library.. looking at Liferay code, I saw that they are accessed using the following URL

https://portal.bsc.gwu.edu/c/message_boards/get_message_attachment?messageId=1203065&attachment=yoga.jpg

(where yoga.jpg is the name of the image attached)
I thought that if I can upload the images with the same name as the messageId then I could retrieve them directly using HTTPS, and that action requires authentication (and this is not a JSON web service).. so that is why I decided to store and retrieve the images in the document library instead.. I will try your suggestion, using WEBDEV to access them (the workaround that I used was to make the images publich, guest permission, and access them via HTTP like I did for the avatar images)

btw,I tried to find a method to find messages (message board) by tag and I think the only method would be "/assetentry/get-entries", which its only parameter is the very generic field entryQuery (com.liferay.portlet.asset.service.persistence.AssetEntryQuery)
I did build the whole object in JSON format but it didn't work.. I was debugging and it fails in this class
21:44:15,297 ERROR [http-bio-8080-exec-10][JSONWebServiceServiceAction:114]
looking at the code.. seems that there is no Liferay class register in the JODD json transformers for this Java Class
thumbnail
Abhishek Suthar, modificado hace 10 años.

RE: Android App getting user's avatar image (or message's attachemnts)

New Member Mensajes: 18 Fecha de incorporación: 26/06/13 Mensajes recientes
Hi Zenobia,

Can you please inform me how you fetch user image from liferay in mobile app.
I tried to fetch image using potrait service. I have created custom service in liferay which gives back url of user's image. I need to use async task for each and every custom service call to liferay.

Here is my sample code .
class TheTask extends AsyncTask<void, void,string> {
	String potraitId;
	User _user;
	TheTask(User _user)
	{
		this._user = _user;
	}
	

	 protected String doInBackground(Void... params) {
		 Session session = SettingsUtil.getSession();
			MobileappService mobileappService  = new MobileappService(session);
			try {
				potraitId = mobileappService.getPotraitId(_user.getUserId());//This is my custom liferay web service
				System.out.println("&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;++++++++"+potraitId); //I am able to get user's potraidId successfully, but get error when I tried to fetch users ImageURL using Potrait service
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return null;
			
	 }

	 
	 }</void,>


Can you please you code for fetching the user's image. Please let me know if I am doing something wrong.
Zenobia L, modificado hace 10 años.

RE: Android App getting user's avatar image (or message's attachemnts)

Junior Member Mensajes: 28 Fecha de incorporación: 8/04/11 Mensajes recientes
Hi Abhishek:

Sure..
My Android app is connecting to a Liferay 6.1 GA2 server, then I am not using directly the SDK provided by Liferay because it is supposed to be for Liferay 6.2, but I took a look at its source code and did calls in a similar way

Most of the methods in the Liferay Android SDK are creating their own Asynctask in Android

here is my code

(1) first I read all the message's info without images (JSON call to Liferay in an Asynctask), to display first the text..
(2) then I go and get the images for each message (lazy load) (I am using a ViewList and an Adapter)
, using an Asynctask for each call

(2) Here is my code in the Async Task


protected ParamHolder doInBackground(ParamHolder... params) {
		try {
                                 // get message's image
			int messageId = params[0].messageId;
			MessageBoardService service = new MessageBoardService(session);
			Bitmap image = service.getMessageImage(messageId);
			params[0].imageBitmap = image;
			
			// get userImage
			Bitmap userImage = service.getUserImage(params[0].userMessageId);
			params[0].userImageBitmap = userImage;

		} catch (Exception e) {
			return null;
		}
		return params[0];

	}
protected void onPostExecute(ParamHolder param) {
		if (param != null &amp;&amp; param.imageBitmap != null) {
			param.imageView.setImageBitmap(param.imageBitmap);
			param.imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);	
			values[param.position].setMessageImage(param.imageBitmap);

		}
		
		if (param != null &amp;&amp; param.userImageBitmap != null) {
			param.userImageView.setImageBitmap(param.userImageBitmap);
			param.userImageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
			values[param.position].setUserImage(param.userImageBitmap);

		}

	}



Here is the method that gets the image for the user



	public Bitmap getUserImage(int userId) throws Exception {
		
		int portraitId = LiferayUserInfo.getImageId(userId);             
                              // (I found out that regardless of the user's gender, 
                              //  I can use the user_male_portrait subdirectory or the user_female_portrait subdirectory
		String imageURL = LiferayServerInfo.SERVER_ADDRESS
				+ "/image/user_male_portrait?img_id=" + portraitId;
		Bitmap image = LiferayClientUtil.getBitmapFromURL(imageURL);

		return image;
             }	




public static Bitmap getBitmapFromURL(String link) {
	    try {
	        URL url = new URL(link);
	        HttpURLConnection connection = (HttpURLConnection) url
	                .openConnection();
	        connection.setDoInput(true);
	        connection.connect();
	        InputStream input = connection.getInputStream();
	        Bitmap myBitmap = BitmapFactory.decodeStream(input);

	        return myBitmap;

	    } catch (IOException e) {
	       Log.e("LIFERAYSERVICE", "link " + link);
	        Log.e("LIFERAYSERVICE", "bitmap download " + e.getMessage());
	        return null;
	    }
	}



Hope it helps
thumbnail
Abhishek Suthar, modificado hace 10 años.

RE: Android App getting user's avatar image (or message's attachemnts)

New Member Mensajes: 18 Fecha de incorporación: 26/06/13 Mensajes recientes
Hi Zenobia,

Thank you very much for your reply.That really helped. emoticon

So this means that if we use liferay mobile sdk for building custom services then we do not have to do Asynctask explicitly in my java code as I have mentioned earlier ??
Is that correct??

We are using mobile sdk for building jar for custom services.
and If we make two call in method then its giving that ExceptionOnMainThread which related to calling more then one network call in one thread..... why????

Thanks,
Abhishek
Zenobia L, modificado hace 10 años.

RE: Android App getting user's avatar image (or message's attachemnts)

Junior Member Mensajes: 28 Fecha de incorporación: 8/04/11 Mensajes recientes
Hi Abhishek,

Since I am not using the SDK, I can not tell from experience,
but from reading the code, seems that is one of the options

for example, if you take a look at this method, from the AssetTagService (Liferay Mobile SDK)


public JSONArray getTags(String className, long classPK) throws Exception {
		JSONObject _command = new JSONObject();

		try {
			JSONObject _params = new JSONObject();

			_params.put("className", className);
			_params.put("classPK", classPK);

			_command.put("/assettag/get-tags", _params);
		}
		catch (JSONException _je) {
			throw new Exception(_je);
		}

		return (JSONArray)session.invoke(_command);
	}



it calls session.invoke.. which can create the AsyncTask in Android

 
public Object invoke(JSONObject command) throws Exception {
		if (callback != null) {
			ServiceAsyncTask task = new ServiceAsyncTask(this, callback);

			task.execute(command);

			return null;
		}
		else {
			JSONArray json = HttpUtil.post(this, command);

			return json.get(0);
		}
	}



I found more info at:
https://www.liferay.com/documentation/liferay-portal/6.2/development/-/ai/using-the-android-sdk-liferay-portal-6-2-dev-guide-08-en

I am creating my own AsyncTasks and JSON call since I am connecting to 6.1
thumbnail
Abhishek Suthar, modificado hace 10 años.

RE: Android App getting user's avatar image (or message's attachemnts)

New Member Mensajes: 18 Fecha de incorporación: 26/06/13 Mensajes recientes
Hey thanks,

That worked....emoticonemoticon

Regards,
Abhishek
thumbnail
Bruno Farache, modificado hace 10 años.

RE: Android App getting user's avatar image (or message's attachemnts)

Liferay Master Mensajes: 603 Fecha de incorporación: 14/05/07 Mensajes recientes
Hi Abhishek,

The Mobile SDK provides an asynchronous way of calling services, you don't need to create an AsyncTask, take a look at the docs.
thumbnail
Abhishek Suthar, modificado hace 10 años.

RE: Android App getting user's avatar image (or message's attachemnts)

New Member Mensajes: 18 Fecha de incorporación: 26/06/13 Mensajes recientes
Hi Bruno,

Thank you very much for your reply..

Actually I want to confirm that Is it necessary to follow below step for calling custom service.....

1) Create Async allback
AsyncTaskCallback callback = new JSONArrayAsyncTaskCallback() {

public void onFailure(Exception exception) {
// Implement exception handling code
}

public void onSuccess(JSONArray result) {
// Called after request has finished successfully
}

};


2) set it to session.
session.setCallback(callback);

3) then call my customservice.
service.getGroupEntries(10184, 0, 0, -1, -1);


Thanks in advance.

Regards,
Abhishek
Zenobia L, modificado hace 10 años.

RE: Android App getting user's avatar image (or message's attachemnts)

Junior Member Mensajes: 28 Fecha de incorporación: 8/04/11 Mensajes recientes
just wanted to report that I am now downloading and uploading secured images from Android to Liferay document library..
using WebDAV URLs provided by Liferay

Here is the client put method using HttpClient and targeting Liferay WebDAV services

public static int  uploadImage(Bitmap image, String baseUrl, String userId,
			String password) throws Exception {
		
		// converting image to JPG
		ByteArrayOutputStream jpgPhoto = new ByteArrayOutputStream();
		int quality = 100; // max quality
		image.compress(Bitmap.CompressFormat.JPEG, quality, jpgPhoto);
		byte [] photo = (byte [])jpgPhoto.toByteArray();				
				
		UsernamePasswordCredentials	credentials = new UsernamePasswordCredentials(userId, password);
		ByteArrayEntity photoEntity = new ByteArrayEntity(photo) ;
		photoEntity.setContentType("image/jpeg");

		HttpPut put = new HttpPut(baseUrl);
		put.setEntity(photoEntity );

		DefaultHttpClient client = new DefaultHttpClient();

		HttpConnectionParams.setConnectionTimeout(client.getParams(),DEFAULT_CONNECTION_TIMEOUT);
		client.getCredentialsProvider().setCredentials(AuthScope.ANY,credentials);

		HttpResponse response = client.execute(put);
		// Handle Response status		
		int status = response.getStatusLine().getStatusCode();
		return status;


	}