In webview, we need to obtain the favicon. ICO icon of the website. However, by default, the icon obtained by the onreceivedicon method in webchromeclient is always NULL;
Webview. getfavicon (); returns NULL. This is strange, so
After some Google attacks, we found that foreigners also encountered this problem.
========================================================== ====================
Http://stackoverflow.com/questions/3462582/display-the-android-webviews-favicon
I 'd like to display the favicon of the website I am accessing via the Android. WebKit. webview. I 've tried two ways to get it:
1)WebViewClient.onPageStarted()Method Has A favicon parameter that is always null.
2)WebChromeClient.onReceivedIcon()Method is never called.
3) calledWebView.getFavicon()InonPageStarted()AndonPageFinished()But it always returns NULL.
I haven't been able to find an example online that shows how to access the favicon. Any hints wocould be greatly appreciated.
One of the answers solves this problem.
For the webview icon methods and listeners to work, you need to first open the webicondatabase manually. You wowould typically do this inonCreate()Method of your activity.
Try adding the following lineonCreate():
WebIconDatabase.getInstance().open(getDir("icons", MODE_PRIVATE).getPath());
Once you 've done this, you should start gettingonReceivedIcon()Callbacks for any webview in this activity, andgetFavicon()Method shoshould also start returning a valid object rather than Null whenever icons are available.
========================================================== ======================================
Before using the getfavicon () method in webview, you must first call the oncreate () method.
(WebIconDatabaseThat is, the icon database management object. All webviews request the same icon database object .)
Webicondatabase. getinstance (). Open ("directory for storing icons ");
This directory for storing icons is customized. It is usually placed in the data/package name/Cache/directory of the corresponding project.
I developed a quick browser for my own hands-on applications fast browser http://www.ifeisu.com
The project is located
/Data/COM. ifeisu. Browser/Cache/icons/
Code in oncreate:
Webicondatabase. getinstance (). Open (util. getdirs (getcachedir (). getabsolutepath () + "/icons /"));
Code for creating a directory:
Public static string getdirs (string path)
{
File dir = new file (PATH );
If (! Dir. exists ()){
Dir. mkdirs ();
}
Return path;
}
The premise is that your icons directory must be created in advance, or an error will be reported.
The answer from a foreigner
getDir("icons", MODE_PRIVATE).getPath()
It is the context method. I don't use it here. I use the custom method directly.
Let's take a look at the results.