安卓WebView中点击图片事件

     mWebviewMall.setWebViewClient(new WebViewClient() {//不跳转至浏览器,加载开始和完成监听


            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                  view.loadUrl(url);
                if (allImgURLS.contains(url)){
                    view.stopLoading();
                    csh.openImage(allImgURLS, url);
                    return false;
                }
                Log.e("TAGresponse===?", url);
                   if (url.startsWith("http:") || url.startsWith("https:")) {
//                    view.loadUrl(url);
                       if (url.startsWith("weixin://wap/pay?")) {
                           Intent intent = new Intent();
                           intent.setAction(Intent.ACTION_VIEW);
                           intent.setData(Uri.parse(url));
                           startActivity(intent);
                           return true;
                       } else {
                           Map extraHeaders = new HashMap();
                           extraHeaders.put("Referer", Constants.BOE_URL);
//                           view.loadUrl(url, extraHeaders);
//                           return true;

                       }
                    return true;
                } else {
                    try {
                        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                        startActivity(intent);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    return true;
                }


            }


            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
//                if (mDialog == null)
//                    mDialog = DeveloperUtils.createLoadingDialog(ShopGoodsWebActivity.this, "正在加载,请稍候");
//                mDialog.show();

                pb.startProgress();
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                String js = "var obj = document.getElementsByTagName(\"img\");" +
                        "var o = '';" + " for(var i = 0; i < obj.length;i++){ o += obj[i].src+\"|\";" +
                        "obj[i].onclick = function(){" +
                        "document.location = this.src;" +
                        "}" +
                        "}";
                mWebviewMall.loadUrl("javascript:" + js);

                String jsGetImages = "function getImages(){var objs = document.getElementsByTagName(\"img\");var imgScr = \"\";for(var i=0;i() {
                    @Override
                    public void onReceiveValue(String s) {
                        if (s.startsWith("\"")){
                            s = s.substring(1);
                        }
                        if (s.endsWith("\"")){
                            s = s.substring(0, s.length()-1);
                        }
                        if (s.endsWith("|")){
                            s = s.substring(0, s.length()-1);
                        }
                        LogUtils.e("s", s);
                        allImgURLS = s;
                    }
                });

//                mDialog.dismiss();
                pb.finishProgress();
                pb.setVisibility(View.GONE);
            }
        });
 @JavascriptInterface
        public void openImage(String url, String src) {
            array = url.split("\\|");
            stringList = new ArrayList();
            int po = 0;
            for (int i = 0; i < array.length; i++) {
                if (array[i].equals(src)) {
                    po = i;
                }
                stringList.add(array[i]);
            }
            imageBrower(po, (ArrayList) stringList);
        }

 

你可能感兴趣的:(安卓WebView中点击图片事件)