5

I'm working on android and trying to download and display a favicon(.ICO) from a website on an ImageView.

So far I've manage to read the .ico from a website using an HTTP connection, retrieve it as an InputStream. Then I use a BitmapFactory to decode the stream into a Bitmap and display it on the ImageView. Here's the code:

 public Bitmap getBitmapFromURL(URL src) {           
        try {

            URL url = new URL("http", "www.google.com", "/favicon.ico");

            HttpURLConnection connection = (HttpURLConnection) url.openConnection();                

            connection.setDoInput(true);       

            connection.connect();               

            InputStream input = connection.getInputStream();    

            BitmapFactory.Options options = new BitmapFactory.Options();

            Bitmap myBitmap = BitmapFactory.decodeStream(input, null, options);  

            return myBitmap;

        } catch (IOException e) {               
            e.printStackTrace();                
            return null;                
        }
    }

The problem is that the decoding of the inputStream always returns a small 16x16 Bitmap. If I well understood, a single .ICO file can store different image resolutions, like 32x32 and 64x64. My question is, is there a way to decode the 32x32 or the 64x64 Bitmap instead of the 16x16?

Also, if there isn't a solution with BitmapFactory, is there a library or java code to do this?

NOTE: I don't want to resize the Bitmap, I want a 32x32(or bigger) resolution without losing the image quality by stretching.

Thanks in advance.

David Neto
  • 809
  • 1
  • 12
  • 20
  • Maybe this specific .ico only contains a 16x16 version? – Ilya Kogan Sep 07 '13 at 23:31
  • I don't think so, if you type www.google.com/favicon.ico on your browser it presents you a 32x32 image. Unless I'm missing something, it shows that at least a 32x32 image is encoded in the file. The BitmapFactory is either, for some reason, scaling down the 32x32 version, or there is another version, a 16x16 also encoded in the file and the BitmapFactory is chosing to decode that one. – David Neto Sep 07 '13 at 23:48
  • @DavidNeto I didn't know you could read and decode an Ico file this way. Can you actually display that 16x16 image you get? – quinestor Sep 20 '13 at 12:08
  • 1
    Look on this answer http://stackoverflow.com/a/35957573/1554094 May be it helps you. – Alexander Apr 26 '16 at 11:30

2 Answers2

2

I know the question was asked 3 years ago, but I have faced the very same problem and adapted a portion of image4j into ico4a, which you can find here: https://github.com/divStar/ico4a (I wrote it myself because I wanted to load the biggest image from a favicon; since image4j uses AWT-classes, which are not easily available for android, I made it so ico4a mostly uses native android classes).

It can decode most valid ICO-files into a List of Bitmap-objects. I believe either the library itself or the sample application has also a method to retrieve the biggest Bitmap-object.

However, my library is not able to write ICO-files; it can only read them. You can save the individual images as PNG or JPEG graphics easily though using some built-in android functionality.

Igor
  • 1,582
  • 6
  • 19
  • 49
1

The favicon file may contains mutliple icons, usually one in 16x16 and one in 32x32. This is discussed in the following thread : How to have multiple favicon sizes, yet serve only a 16x16 by default?

It is the case with the Google's favico. If you try to download the file www.google.com/favicon.ico and open it with an application like IrfanView, you can see that there are two icons (16x16 and 32x32).

To answer your question, you should use a proper library to extract multiple icons, like image4j, and choose the one you need.

http://image4j.sourceforge.net/

Community
  • 1
  • 1
Thire
  • 437
  • 4
  • 10
  • 1
    Thanks for your answer. Unfortunately, the library doesn't work in android. Apparently it uses some classes, for example BufferedImage, which belongs to the java library java.awt.image, which doesn't exist on android. From what I've understand, this library is supposed to control the graphics handling, and android does it on a different way. – David Neto Sep 11 '13 at 14:52
  • You're right, you need a similar library for Android, and I don't know if there is one. Maybe have a look at this question: [Can BitmapFactory.decodeFile handle .ICO (Windows icons) files?](http://stackoverflow.com/a/2690923/2482928), which mentions that there is an Android source for ICO parsing. You could use that to parse your favicon. – Thire Sep 12 '13 at 08:25