android Webview读取网页里的所有图片
Posted mo_weifeng
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了android Webview读取网页里的所有图片相关的知识,希望对你有一定的参考价值。
<WebView
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
初始化,这里读的是文本
@SuppressLint("javascriptInterface")
private void initWebView(String html)
// 启用javascript
contentWebView.getSettings().setJavaScriptEnabled(true);
contentWebView.getSettings().setDefaultTextEncodingName("UTF-8");
// // 随便找了个带图片的网站
// contentWebView.loadUrl("https://www.pmcaff.com/discuss/3394893931276352?newwindow=1");
contentWebView.loadDataWithBaseURL(null, html, "text/html", "utf-8", null);//加载html数据
// 添加js交互接口类,并起别名 imagelistner
// contentWebView.addJavascriptInterface(new JavascriptInterface(this), "imagelistner");
contentWebView.addJavascriptInterface(new JavascriptInterface2(this), "imageListener");
contentWebView.setWebViewClient(new MyWebViewClient());
// 监听
private class MyWebViewClient extends WebViewClient
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
return super.shouldOverrideUrlLoading(view, url);
@Override
public void onPageFinished(WebView view, String url)
view.getSettings().setJavaScriptEnabled(true);
addImageClickListner2();
super.onPageFinished(view, url);
// html加载完成之后,添加监听图片的点击js函数
// addImageClickListner();
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon)
view.getSettings().setJavaScriptEnabled(true);
super.onPageStarted(view, url, favicon);
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
super.onReceivedError(view, errorCode, description, failingUrl);
// 注入js函数监听
private void addImageClickListner2()
//网页加载完成 走JS代码
contentWebView.loadUrl("javascript:(function()" +
"var objs = document.getElementsByTagName(\\"img\\"); " +
"var imgUrl = \\"\\";"+
"var filter = [\\"img//EventHead.png\\",\\"img//kong.png\\",\\"hdtz//button.png\\"];"+
"var isShow = true;"+
"for(var i=0;i<objs.length;i++)" +
"for(var j=0;j<filter.length;j++)"+
"if(objs[i].src.indexOf(filter[j])>=0) "+
"isShow = false; break;"+
"if(isShow && objs[i].width>1)"+
"imgUrl += objs[i].src + ',';isShow = true;"+
" objs[i].οnclick=function() " +
" "
+ " window.imageListener.openImage(imgUrl,this.src);" +
" " +
"" +
"" +
")()"
);
// js通信接口
public class JavascriptInterface2
private Context context;
public JavascriptInterface2(Context context)
this.context = context;
@android.webkit.JavascriptInterface
public void openImage(String imageUrl,String img)
int position = 0;
String[] imgs = imageUrl.split(",");
ArrayList<String> imgUrlList = new ArrayList<>();
for (String s : imgs)
imgUrlList.add(s);
for (int i = 0;i<imgUrlList.size();i++)
if (img.equals(imgUrlList.get(i)))
position = i;
ImageHandlerUtil.browseImages(getContext(), imgUrlList, position);
// Intent intent = new Intent(MainActivity.this, ImageActivity.class);
// intent.putExtra("number",position);
// intent.putExtra("list",(Serializable) imgUrlList);
// startActivity(intent);
以上是关于android Webview读取网页里的所有图片的主要内容,如果未能解决你的问题,请参考以下文章