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
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.
SKIA library provides decoder class for ICO file. I was able to display an ICO file in the emulator. Haven't tried it yet in an actual android device though.
Bitmap bmap = BitmapFactory.decodeFile("/sdcard/vlc.ico");
The
WebView
component has agetFavicon()
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:
The problem may be down to the fact that I'm not adding the
WebView
to a visibleView
. 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.
I had a similar problem. BitmapFactory.decode decoded *.ico on emulator but not on my Galaxy S. Solution for me was:
No accepted answer util now, I will share my findings here.
Windows .ico file format is a little complicated, it might contains one or more small images at multiple sizes and color depths, such that they may be scaled appropriately. Refer ICO_(file_format)
So when using unix "file" command to check the icon file type, you might get the following result:
Note a.ico and b.ico contains different number of icons.
I tried to use BitmapFactory.decodeFile to decode these icons.
As I only have limited Android devices, I can't give any conclusion. Maybe anyone else could help.
So if you really want to decode .ico files, you may try: