views:

377

answers:

2

Hi all,

I am working on Android. Trying to display the "favicon" (aka "shortcut icon") or web pages.

So I already have the code to get the URL of this icon from any website, and download it onto my Android.

Now I am trying to make a Bitmap out of it, but always get null as a result.

The code:

String src = String.format("file:///data/data/com.intuitiveui.android/files/%s",prefName); // prefName is "facebook.ico", and I do see tht file in my DDMS file browser. It's // a valid icon file.

Bitmap res = BitmapFactory.decodeFile(src); // This returns null

TIA

+1  A: 

Here is a list of the supported Android media formats. ICO is not among them. You might be able to find a Java-based ICO decoder, though.

CommonsWare
Thanks. That's what I thought, too. But then I saw that WebView interface, so I thought I'd give it a try.
Shimon Shnitzer
A: 

The WebView component has a gevFavicon() method so it's definitely possible to decode ICO files in Android. You could have a look at the Android source to see how ICO files are parsed. I've had a quick look but can't find the relevant part.

Alternatively, you should be use the SDK to get favicons for you. However, I've had a quick try and can't get it to work.

For what it's worth here's my test code, noting again that this doesn't work:

    String url = "http://stackoverflow.com";
    WebView wv = new WebView(this);
    wv.setWebViewClient(new WebViewClient() {
        @Override
        public void onPageFinished(WebView view, String url) {
            Log.i("HelloAndroid","Loaded " + url);              
            Log.i("HelloAndroid","Icon " + (view.getFavicon() == null ? "not" : "") + " found");
        }

    });
    WebIconDatabase wid = WebIconDatabase.getInstance();
    wid.requestIconForPageUrl(url, new WebIconDatabase.IconListener() {
        public void onReceivedIcon(String url, Bitmap icon) {
            Log.i("HelloAndroid","Found Icon for " + url);
        }
    });
    wv.loadUrl("http://stackoverflow.com");
    Log.i("HelloAndroid","Loading " + url);

The problem may be down to the fact that I'm not adding the WebView to a visible View. If you do get this to work I'd be interested to hear what you did.

So sorry for giving two half complete answers, but I thought it was worth posting what I found.

Dave Webb
Hi,The funny thing is - I did manage to display the ICO file using:Bitmap bmp = BitmapFactory.decodeStream(in);when in is an InputStream of an HttpURLConnection.This renders the ICO file fine.But when I try to first save the ICO to a file:FileOutputStream f = openFileOutput("facebook.ico", MODE_WORLD_READABLE); then reading from that in (InputStream) into this OutputStream,I do see a file facebook.ico created.But then when I try to callBitmap bmp = BitmapFactory.decodeFile("file:///data/data/com.intuitiveui.android/files/facebook.ico");it returns null.
Shimon Shnitzer
Hi,I am aware of the WebView stuff. read somewhere it does not work.Thanks !
Shimon Shnitzer
@Shimon Shnitzer - Perhaps the problem is to do with how the files are handled rather than decoding the data. What happens if use `Bitmap bmp = BitmapFactory.decodeStream(openFileInput("facebook.ico"));`
Dave Webb
It works !!!Thanks !
Shimon Shnitzer
Now only thing that does not work is for some websites my code can find the ICO file info, and for some not.For example - if I try www.facebook.com I can find the "shortcut icon" field and read the ICO location from there (you can try this by browsing to www.facebook.com on your PC, then view html source).For other websites - e.g. www.google.com - I can't find the "shortcut icon" field at all...
Shimon Shnitzer
By default the Favicon is at /favicon.ico - e.g. http://www.google.com/favicon.ico
Dave Webb
Thanks. Seems to work for Google and facebook and...But is this the standard ?Or do i just first check this, and if it's not there - try to read the link ?
Shimon Shnitzer
I would check for the link and if no location is specified look for the FavIcon it in the root directory. Have a look at Wikipedia for more information: http://en.wikipedia.org/wiki/Favicon
Dave Webb
Thanks. Will do
Shimon Shnitzer