19

I am in the process of creating an activity where I will show the list of website visited with its logo and its alias name chosen by user.

e.g.

  • Recent Websites Visited

    logo1 website1/alias name

    logo2 website2/alias name

    .

    . so no

The question is, (Ref. attached image) How to get website logo displayed on left side of http://?

Like below I want to just grab the icon and save it locally

8 Answers 8

22

Use this website:

https://besticon-demo.herokuapp.com/allicons.json?url=www.stackoverflow.com

It will find all logos for a website in multiple sizes and return a nice json string with meta data including the url to the icon. You simply replace www.stackoverflow.com with your domain.

The site also has a gui for entering in websites manually if you prefer:

https://besticon-demo.herokuapp.com/

Here is a sample string returned from querying for the stack overflow website:

{
   "url":"www.stackoverflow.com",
   "icons":[
      {
         "url":"http://stackoverflow.com/apple-touch-icon.png",
         "width":158,
         "height":158,
         "format":"png",
         "bytes":3445,
         "error":null,
         "sha1sum":"c78bd457575a3221c6b3d0d17ffb00ffc63d7cd0"
      },
      {
         "url":"http://cdn.sstatic.net/Sites/stackoverflow/img/favicon.ico?v=4f32ecc8f43d",
         "width":32,
         "height":32,
         "format":"ico",
         "bytes":5430,
         "error":null,
         "sha1sum":"4f32ecc8f43d0986b9c6ce9f37999e86c0b829ef"
      },
      {
         "url":"http://stackoverflow.com/favicon.ico",
         "width":32,
         "height":32,
         "format":"ico",
         "bytes":5430,
         "error":null,
         "sha1sum":"4f32ecc8f43d0986b9c6ce9f37999e86c0b829ef"
      }
   ]
}
2
  • @JackFairfield How it is possible to get this icon and to show in ImageView with Glide May 28, 2019 at 8:52
  • 7
    Site appears down as of August 2020.
    – tjberte
    Aug 1, 2020 at 8:38
22

I know I am late but this API will help others

Android doesn't support favicon files natively, you can fetch favicon in different ways but can't show/use it.

Google provides free API to get favicon in image format.

https://www.google.com/s2/favicons?sz=64&domain_url=microsoft.com

Use Picasso to show fetched favicon in an image view.

2
  • 1
    this will only return a value if the icon has a 64px image set, it could easily have a 16 or 32 instead one wouldn't know.
    – Ricky-U
    Nov 24, 2021 at 17:12
  • This is what I exactly want.
    – iku
    Dec 4, 2021 at 6:31
16

It's called a favicon, and all you have to do is:

  1. If there's an icon at /favicon.ico, use that.
  2. Otherwise, get the content of the page, and extract the location from <link rel="shortcut icon" href="URL goes here" />. You’ll need to use an HTML parser and find the <link> with a rel of either icon or shortcut icon.
3
  • Thanks much for your advise, I will implement and let you know my findings. -Best
    – mask
    May 4, 2012 at 22:30
  • 8
    I got it working with the help of google url provided for this approach. google.com/s2/favicons?domain=www.domain.com to grab the favIcon. Still struggling with background to make transparent as some of them are with white BKG and other one with black. Not sure how to handle those background dynamically. @minitTech Thanks for your help it got me started.
    – mask
    May 15, 2012 at 22:18
  • @mask Your code it is working but the icon it is smaller do you know how ti make bigger and do not crash the favicon size. Jan 19, 2019 at 13:12
8

Use this logo.clearbit.com/stackoverflow.com

You can even customize to get particular size and grascaled version

logo.clearbit.com/stackoverflow.com?size=80&greyscale=true

2
  • Just checked. Clearbit doesn't provide this feature in this way anymore.
    – Kir Mazur
    Feb 23, 2023 at 12:09
  • Looks like it is back - clearbit.com/logo
    – blackhaj
    May 1, 2023 at 21:54
3

This method can be used to get Favicon Icon bitmap

 private Bitmap fetchFavicon(Uri uri) {
        final Uri iconUri = uri.buildUpon().path("favicon.ico").build();
        Log.i(TAG, "Fetching favicon from: " + iconUri);

        InputStream is = null;
        BufferedInputStream bis = null;
        try
        {
            URLConnection conn = new URL(iconUri.toString()).openConnection();
            conn.connect();
            is = conn.getInputStream();
            bis = new BufferedInputStream(is, 8192);
            return BitmapFactory.decodeStream(bis);
        } catch (IOException e) {
            Log.w(TAG, "Failed to fetch favicon from " + iconUri, e);
            return null;
        }
    }
2
  • In the new API the conn.connect will crash the application. Do you have any new suggest ? May 28, 2019 at 9:04
  • while doing network operation always use AsyncTask. Jul 20, 2020 at 13:32
1

Try using this code:

imageview1.setImageBitmap(webview1.getFavicon());
0

Here's a python library which tries to infer the logo image from a URL:

https://github.com/dcollien/urlimage

it parses the HTML at the url, and tries a whole bunch of things including:

  • meta tag with itemprop="image" or property="image"
  • meta tag with property="og:image:secure_url" or property="og:image"
  • meta tag with name="twitter:image"
  • meta tag for Microsoft tiles, with: name="msapplication-wide310x150logo", name="msapplication-square310x310logo", name="msapplication-square150x150logo", name="msapplication-square70x70logo"
  • link tag with rel="apple-touch-icon"
  • link tag with rel="icon"
  • tries out "{scheme}://{domain}/favicon.ico" to see if it exists
  • otherwise pulls out the first img tag (next to an h1)
0

Clearbit have an API that can help with this. It's currently free if you attribute the link.

The docs for it are hard to find but here is an FAQ answer with details.

In short, you make a get request as below where :domain is the domain of the site you want the logo from:

https://logo.clearbit.com/:domain

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.