This blog post shows how the avatar image of the user is displayed in the feedback section using the Gravatar service on SUSI.AI Android app. A Gravatar is a Globally Recognized Avatar. Your Gravatar is an image that follows you from site to site appearing beside your name when you do things like comment or post on a blog. So, it was decided to integrate the service to SUSI.AI, so that it helps to identify the user via the avatar image too.
- The aim is to use the user’s email address to get his/her avatar. For this purpose, Gravatar exposes a publicly available avatar of the user, which can be accessed via following steps :
- Creating the Hash of the email
- Sending the image request to Gravatar
- For the purpose of creating the MD5 hash of the email, use the MessageDigest class. The function takes an algorithm such as SHA-1, MD5, etc. as input and returns a MessageDigest object that implements the specified digest algorithm.
Perform a final update on the digest using the specified array of bytes, which then completes the digest computation. That is, this method first calls update(input), passing the input array to the update method, then calls digest().
- Convert the byte array to String, which is the requires MD5 hash of the email string.
- Now, a URL is generated using the hash. This is the URL that will be used to fetch the avatar.
- The URL format is https://www.gravatar.com/avatar/HASH, where HASH is the hash of the email of the user. In case, the hash is invalid, Gravatar returns a placeholder avatar.
- Also, append ‘.jpg’ to the URL to maintain image format consistency in the app.
- Finally, load this URL using Picasso and set it in the appropriate view holder.
You can now see the avatar image of the users in the feedback section. 😀
- MessageDigest | Android Developers