StackTips

How to Generate Gravtar Image Url from Email in Java

stacktips avtar

Written by

Editorial,  1 min read,  2.71K views, updated on Sept. 17, 2023

The following code snippet shows how to generate Gravatar URLs from email address. This utility method allows 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;
}

Beginner's Guide to Java Collections

This course covers the fundamentals of java collections framework covering different data structures in Java to store, group, and retrieve objects.

>> CHECK OUT THE COURSE
stacktips avtar

Editorial

StackTips provides programming tutorials, how-to guides and code snippets on different programming languages.

Related posts

Let’s be friends!

🙌 Stay connected with us on social media for the latest updates, exclusive content, and more. Follow us now and be part of the conversation!