Display image responses in SUSI.AI Android app
SUSI.AI Android app has many response functionalities ranging from giving simple ANSWER type responses to complex TABLE and MAP type responses. Although, even after all these useful response types there were some missing action types all related to media. SUSI.AI app was not capable of playing any kind of image responses.So, to do this the links received in the response were used to fetch the corresponding image stored in the link.
Since, the app now has two build flavors corresponding to the F-Droid version and PlayStore version respectively it had to be considered that while adding the feature to display images any proprietary software was not included with the F-Droid version.
The JSON response from the server whenever a query for was asked gave the link to the image. For eg : on querying : “Image of cat “ the server gave the response as :
Actions”: [
{
“language”: “en”,
“type”: “answer”,
“expression”: “https://pixabay.com/get/e830b1072ef5023ed1584d05fb1d4790e076e7d610ac104496f0c77ea0e9bcbf_640.jpg”
}
So in the android app just like the usual answer type response a message was displayed with the link to the image present in the message.
Catching IMAGE response :
The image responses are of the type “answer” as seen in the server response above. So we a method had to be devised so that the responses which display the images are caught and displayed. In the file ChatFeedRecyclerAdapter.java a separate code to detect the IMAGE response was added as :
private static final int IMAGE = 17; |
Next we must specify that what type of view that is to be used whenever an IMAGE response is encountered by the app. Since the action type is “answer” a specification was required to choose the type of the viewholder. Since the images are only displayed through the pixabay the URL of the images end with either “.jpg” or “.png”. So in the expression of the response if we check that it is a link and also it ends with either “.jpg” or “.png” it will be certain that the response given from the server is an image.
The code to identify the view type :
@Override public int getItemViewType(int position) { ChatMessage item = getItem(position); if (item.getId() == -404) return DOTS; else if (item.getId() == -405) return NULL_HOLDER; else if (item.isDate()) return DATE_VIEW; else if (item.getContent().endsWith(“.jpg”) || item.getContent().endsWith(“.png”)) return IMAGE; |
Inflating the layout of type IMAGE
Now after determining that the response will be an image we have to inflate the layout of the viewholder to support images in the onCreateViewHolder() method . The layout of the image response was inflated as follows :
case IMAGE: view = inflater.inflate(R.layout.image_holder, viewGroup, false); return new ImageViewHolder(view, clickListener); |
Here ImageViewHolder is the view holder that is used for displaying the images , we will discuss it later in the post. Also now in the onBindViewHolder() method of the ChatFeedRecyclerAdapter.java file we have to specify the instance of the view holder if it was to support the image response. It was done as follows :
else if (holder instanceof ImageViewHolder) { ImageViewHolder imageViewHolder = (ImageViewHolder) holder; imageViewHolder.setView(getData().get(position)); } |
ViewHolder for Images :
Like all other viewholders for different variety responses from the server a viewholder to display the images was also needed to be included in the app. So, ImageViewHolder.java was created which handles the images to de displayed in the app. In the setView() method ChatMessage object is passed and this object is used to get the image url so that it is used to display the image using Picasso.
public void setView(ChatMessage model) { this.model = model; if (model != null) { imageURL = model.getContent(); try { Picasso.with(itemView.getContext()) .load(imageURL) .placeholder(R.drawable.ic_susi) .into(imageView); } catch (Exception e) { Timber.e(e); } } |
And on clicking the image we can view the image on full screen using a custom chrome tab as follows :
imageView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder(); CustomTabsIntent customTabsIntent = builder.build(); customTabsIntent.launchUrl(itemView.getContext(), Uri.parse(imageURL)); } }); |
Conclusion :
References :
- Susi server response in case of images : https://api.susi.ai/susi/chat.json?timezoneOffset=-330&q=image+of+cat
- Pixabay is the source used for fetching images : https://pixabay.com
- Display images using Picasso : https://guides.codepath.com/android/Displaying-Images-with-the-Picasso-Library
You must be logged in to post a comment.