How to Generate Gravtar Image Url from Email in Java

The following code snippet shows how to generate Gravatar URLs from email address. This utility method alows you to pass the size of your email.

public String getGravatarUrl(String email, int size) {
    if (null == email) return null;

    final String hash = MD5Util.md5Hex(email.toLowerCase());
    final String gravtar = "http://www.gravatar.com/avatar/%s?s=%s&r=g&d=404";
    final String gravatarUrl = String.format(gravtar, hash, size);

    try {
        return URLEncoder.encode(gravatarUrl, "UTF-8");
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    return null;
}
Tagged in : #Java

Nilanchala

A blogger, a bit of tech freak and a software developer. He is a thought leader in the fusion of design and mobile technologies. He is the author of Xamarin Mobile Application Development for Android Book (goo.gl/qUZ0XV3), DZone MVB and founder of stacktips.com.

Join The Discussion

Please note: We reserve the right to delete comments that contains snarky remarks, offensive or off-topic. To know more read our comments policy.