参考:http://blog.csdn.net/wangtingshuai/article/details/8635787
js代码
// 注入js函数监听 private void addImageClickListner() { // 这段js函数的功能就是,遍历所有的img几点,并添加onclick函数,函数的功能是在图片点击的时候调用本地java接口并传递url过去 contentWebView.loadUrl("javascript:(function(){" + "var objs = document.getElementsByTagName("img"); " + "for(var i=0;i<objs.length;i++) " + "{" + " objs[i].onclick=function() " + " { " + " window.imagelistner.openImage(this.src); " + " } " + "}" + "})()"); }
筛选图片
// 注入js函数监听 private void addImageClickListner() { // 这段js函数的功能就是,遍历所有的img几点,并添加onclick函数,函数的功能是在图片点击的时候调用本地java接口并传递url过去 contentWebView.loadUrl("javascript:(function(){" + "var objs = document.getElementsByTagName("img"); " + "for(var i=0;i<objs.length;i++) " + "{" +" if(objs[i].src) " +" if(objs[i].src.indexOf('display')>0)" + " objs[i].onclick=function(){ " + " window.imagelistner.openImage(this.src); " + " } " + "}" + "})()"); }
全代码
package com.example.testandroid; import android.annotation.SuppressLint; import android.content.Context; import android.content.Intent; import android.graphics.Bitmap; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.util.Log; import android.webkit.WebView; import android.webkit.WebViewClient; public class MainActivity extends ActionBarActivity { WebView contentWebView; @SuppressLint("JavascriptInterface") @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); contentWebView = (WebView) findViewById(R.id.webview); // 启用javascript contentWebView.getSettings().setJavaScriptEnabled(true); // 随便找了个带图片的网站 contentWebView.loadUrl("http://www.qdaily.com/display/articles/7237"); // 添加js交互接口类,并起别名 imagelistner contentWebView.addJavascriptInterface(new JavascriptInterface(this), "imagelistner"); contentWebView.setWebViewClient(new MyWebViewClient()); } // 注入js函数监听 private void addImageClickListner() { // 这段js函数的功能就是,遍历所有的img几点,并添加onclick函数,函数的功能是在图片点击的时候调用本地java接口并传递url过去 contentWebView.loadUrl("javascript:(function(){" + "var objs = document.getElementsByTagName("img"); " + "for(var i=0;i<objs.length;i++) " + "{" // +"window.imagelistner.openImage(objs[i].src)" + " objs[i].onclick=function() " + " { " + " window.imagelistner.openImage(this.src); " + " } " + "}" + "})()"); } // js通信接口 public class JavascriptInterface { private Context context; public JavascriptInterface(Context context) { this.context = context; } public void openImage(String img) { Log.e("mytag", img); } public void log(String s){ Log.e("mytag", s); } } // 监听 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); 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); } } }
openImage 中做图片的处理吧