Java Android WebView getFavicon()返回null
使用后,我正在尝试获取加载页面的图标Java Android WebView getFavicon()返回null,java,android,webview,favicon,Java,Android,Webview,Favicon,使用后,我正在尝试获取加载页面的图标 WebView webView = new WebView(getActivity()); webView.loadUrl("http://" + url); 我正在将异步WebViewClient附加到WebView以在加载favicon后获取它 webView.setWebViewClient(new WebViewClient() { @Override public void onPa
WebView webView = new WebView(getActivity());
webView.loadUrl("http://" + url);
我正在将异步WebViewClient
附加到WebView
以在加载favicon后获取它
webView.setWebViewClient(new WebViewClient()
{
@Override
public void onPageFinished(WebView view, String url)
{
String linkTitle = view.getTitle();
Bitmap favicon = view.getFavicon();
onLinkUrlFinished(url, linkTitle);
}
});
favicon Return始终为空,即使对于像google/facebook这样肯定有favicon的网站也是如此
另一个线程说要使用WebIconDatabase
,但不推荐使用:
android站点上的API指的是根本不存在的WebViewClient.onReceivedIcon
这里发生了什么?关键是打开
WebIconDatabase
,这样WebView
就可以放置图标,并覆盖WebChromeClient.onReceivedIcon
。有关更多信息,请参阅。关键是打开WebIconDatabase
,以便WebView
有地方放置图标,并覆盖WebChromeClient.onReceivedIcon
。有关更多信息,请参阅。因此,最后我没有使用不推荐的API,而是发现如果将/favicon.ico
放在域后,它将为您提供ico
文件,我最后使用该文件获取图像。Uri API将有一个getHost()
方法,该方法将为您提供主机,而无需手动解析它
String faviconUrl = Uri.parse(url).getHost() + "/favicon.ico";
例如,对于google来说,图标url将是www.google.com/favicon.ico,因此最终我没有使用不推荐的API,而是发现如果你在域后放置
/favicon.ico
,它会给你ico
文件,我最后用它来获取图像。Uri API将有一个getHost()
方法,该方法将为您提供主机,而无需手动解析它
String faviconUrl = Uri.parse(url).getHost() + "/favicon.ico";
例如,对于谷歌来说,图标url将是www.google.com/favicon.icoWebIconDatabase从API 19开始就被弃用了。根据规范中的注释: @已弃用此类仅在以下设备上运行时才需要 {@link android.os.Build.VERSION#u code#JELLY_BEAN_MR2} 因此,除非您不想支持API 18及以下版本,否则仍应使用WebIconDatabase:
WebIconDatabase.getInstance().open(getDir("icons", MODE_PRIVATE).getPath());
然后,无论您希望支持哪种API,都需要在自定义WebChromeClient中指定:
public class MyCustomWebChromeClient extends WebChromeClient {
@Override
public void onReceivedIcon(WebView view, Bitmap icon) {
super.onReceivedIcon(view, icon);
// do whatever with the arguments passed in
}
}
请记住在WebView中注册自定义WebChromeClient:
mWebView.setWebChromeClient(new MyCustomWebChromeClient());
自API 19起,WebIconDatabase已被弃用。根据规范中的注释: @已弃用此类仅在以下设备上运行时才需要 {@link android.os.Build.VERSION#u code#JELLY_BEAN_MR2} 因此,除非您不想支持API 18及以下版本,否则仍应使用WebIconDatabase:
WebIconDatabase.getInstance().open(getDir("icons", MODE_PRIVATE).getPath());
然后,无论您希望支持哪种API,都需要在自定义WebChromeClient中指定:
public class MyCustomWebChromeClient extends WebChromeClient {
@Override
public void onReceivedIcon(WebView view, Bitmap icon) {
super.onReceivedIcon(view, icon);
// do whatever with the arguments passed in
}
}
请记住在WebView中注册自定义WebChromeClient:
mWebView.setWebChromeClient(new MyCustomWebChromeClient());
要使用
onReceiveIcon()
,应使用setWebChromeClient
。
这就是我所做的,它为我工作
webView.setWebChromeClient(new WebChromeClient() {
@Override
public void onProgressChanged(WebView view, int newProgress) {
super.onProgressChanged(view, newProgress);
progressBar.setProgress(newProgress);
}
@Override
public void onReceivedIcon(WebView view, Bitmap icon) {
super.onReceivedIcon(view, icon);
webImage.setImageBitmap(icon);
}
});
要使用
onReceiveIcon()
,应使用setWebChromeClient
。
这就是我所做的,它为我工作
webView.setWebChromeClient(new WebChromeClient() {
@Override
public void onProgressChanged(WebView view, int newProgress) {
super.onProgressChanged(view, newProgress);
progressBar.setProgress(newProgress);
}
@Override
public void onReceivedIcon(WebView view, Bitmap icon) {
super.onReceivedIcon(view, icon);
webImage.setImageBitmap(icon);
}
});
我知道这是一个老话题,但是对于那些在使用webview客户端获取favicon时遇到问题的人来说 科特林:
override fun onPageFinished(view: WebView?, url: String?) {
super.onPageFinished(view, url)
tabTitle.text = view?.title // read website title
loadImg(view) // method to load the favicon
}
private fun loadImg (view: WebView?){
// u can directly use tabImage.setBitmap instead of assigning tabImg as val
val tabImg: ImageView = findViewById(R.id.tabImage)
// creating handler object to delay the associated thread a little bit after onPageFinished is called.
val handler = Handler()
val runnable = Runnable {
if(view?.favicon != null) {
tabImg.setImageResource(0) //remove the default image
tabImg.setImageBitmap(view?.favicon) // set the favicon
}
}
handler.postDelayed(runnable, 200) // delay time 200 ms
}
它对我有用,希望它能帮助新读者,如果它能帮助你,请投票,这样你就能帮助别人
致以最诚挚的问候我知道这是一个老话题,但对于那些在使用webview客户端获取favicon时遇到问题的人来说 科特林:
override fun onPageFinished(view: WebView?, url: String?) {
super.onPageFinished(view, url)
tabTitle.text = view?.title // read website title
loadImg(view) // method to load the favicon
}
private fun loadImg (view: WebView?){
// u can directly use tabImage.setBitmap instead of assigning tabImg as val
val tabImg: ImageView = findViewById(R.id.tabImage)
// creating handler object to delay the associated thread a little bit after onPageFinished is called.
val handler = Handler()
val runnable = Runnable {
if(view?.favicon != null) {
tabImg.setImageResource(0) //remove the default image
tabImg.setImageBitmap(view?.favicon) // set the favicon
}
}
handler.postDelayed(runnable, 200) // delay time 200 ms
}
它对我有用,希望它能帮助新读者,如果它能帮助你,请投票,这样你就能帮助别人
向您致意在
清单
中您的最小api是什么?min=14,target=18您正在测试的设备的版本是什么?galaxy Nex上的4.2.1我的想法是这样的WebIconDatabase
仍然需要pre-JellyBean在manifest
中您的最小api是什么?min=14,target=18您正在测试的设备的版本是什么?galaxy Nex上的4.2.1这是我的想法WebIconDatabase
仍然是需要的,如果你甚至阅读了文章tho,我的问题是WebIconDatabase不受欢迎如果你甚至阅读了文章tho,我的问题是WebIconDatabase不受欢迎这不能保证会起作用。例如,返回一个404(截止日期)。@ChrisLacy是的,我知道,但似乎没有更好的替代方案。无法保证这会起作用。例如,返回404(截止到今天)。@ChrisLacy是的,我知道,但似乎没有更好的选择