In Android, when webview receives favicon, it always gets a null solution.

Source: Internet
Author: User

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.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.