如何获得链接URL中的Android的WebView与HitTestResult为链接的图像(而不是

2019-06-27 12:26发布

我试图抓住的WebView longclicks显示上下文菜单。 (见下面的代码),当longclicking的图像,我一直(与IMAGE_TYPE并与SRC_IMAGE_ANCHOR_TYPE链接的图像的未链接的图像)获得的图像URL作为额外的费用。 但我怎么能得到一个超链接的图像链接URL(而不是图像URL)?

最好,塞巴斯蒂安

        mywebview.setOnLongClickListener(new OnLongClickListener() {
            public boolean onLongClick(View v) {

                final WebView webview = (WebView) v;
                final WebView.HitTestResult result = webview.getHitTestResult();

                if (result.getType() == SRC_ANCHOR_TYPE) {
                    return true;
                }

                if (result.getType() == SRC_IMAGE_ANCHOR_TYPE) {
                    return true;
                }

                if (result.getType() == IMAGE_TYPE) {
                    return true;
                }

                return false;
            }
        });

Answer 1:

我检查了的WebView的源代码,它似乎像URI是唯一的额外的数据,你可以得到SRC_IMAGE_ANCHOR_TYPE。 不过,别生气我在这里送给你一个快速和肮脏的解决方法:

    webview.setOnLongClickListener(new OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            final WebView webview = (WebView) v;
            final HitTestResult result = webview.getHitTestResult();
            if(result.getType()==HitTestResult.SRC_IMAGE_ANCHOR_TYPE) {
                webview.setWebViewClient(new WebViewClient(){
                    @Override
                    public boolean shouldOverrideUrlLoading(WebView view, String url) {
                        // 2. and here we get the url (remember to remove the WebView client and return true so that the hyperlink will not be really triggered)
                        mUrl = url; // mUrl is a member variant of the activity
                        view.setWebViewClient(null);
                        return true;
                    }
                });
                // 1. the picture must be focused, so we simulate a DPAD enter event to trigger the hyperlink
                KeyEvent event1 = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_CENTER);
                webview.dispatchKeyEvent(event1);
                KeyEvent event2 = new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DPAD_CENTER);
                webview.dispatchKeyEvent(event2);
                // 3. now you can do something with the anchor url (and then clear the mUrl for future usage)
                String url = mUrl;
                if (url!=null) {
                    Toast.makeText(webview.getContext(), url, Toast.LENGTH_SHORT).show();
                }

                mUrl = null;
            }
            return false;
        }
    });

我试过的代码在低端的Android 2.1设备和高端的Android 4.0的设备,像神韵兼备的工作。

问候

Ziteng Chen



Answer 2:

解决方案中没有上述工作对我来说在Android 4.2.2。 所以,我看着Android默认浏览器的源代码。 我抽出解决这个确切的问题 - 从图片链接获取链接URL。

来源: https://github.com/android/platform_packages_apps_browser/blob/master/src/com/android/browser/Controller.java

萃取溶液:

LongClick监听器:

...
mWebview.setOnLongClickListener(new OnLongClickListener() {

    @Override
    public boolean onLongClick(View v) {
        HitTestResult result = mWebview.getHitTestResult();
        if (result.getType() == HitTestResult.SRC_IMAGE_ANCHOR_TYPE) {
            Message msg = mHandler.obtainMessage();
            mWebview.requestFocusNodeHref(msg);
        }
    }
});
...

处理程序来获取URL:

private Handler mHandler = new Handler() {

    @Override
        public void handleMessage(Message msg) {
            // Get link-URL.
            String url = (String) msg.getData().get("url");

            // Do something with it.
            if (url != null) ...
        }
    };


Answer 3:

我知道这是一个老问题,但我最近碰到这个问题。 基于Perry_ml答案,我用下面的代码科特林解决它:

webView.setOnLongClickListener {
    val result = webView.hitTestResult
    if (result.type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE) {
        val handler = Handler()
        val message = handler.obtainMessage()

        webView.requestFocusNodeHref(message)
        val url = message.data.getString("url")

        // Do something with url, return true as touch has been handled
        true
    } else {
        false
    }
}

我张贴一些关于它的信息在这里 。



Answer 4:

紫藤陈解决方案可高达至Android 4.0(API等级15),但由于某种原因该KeyEvent向下和向上API等级16+(Android 4.1以上版本JELLY_BEAN)不起作用。 它不火的WebView使用loadURL。 所以我不得不更换dispatchTouchEvent的dispatchKeyEvent。 下面的代码:

...
MotionEvent touchDown = MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN, touchX, touchY, 0);
webView.dispatchTouchEvent(touchDown);
touchDown.recycle();

MotionEvent touchUp = MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_UP, touchX, touchY, 0);
webView.dispatchTouchEvent(touchUp);
touchUp.recycle();

String url = mUrl;
...

你可能不得不等待烧制的dispatchTouchEvents后(使用的AsyncTask),以获得mUrl在较慢的设备,其中它的空

希望能帮助到你。



Answer 5:

而不是调用此函数myWebView.requestFocusNodeHref(msg); ,尝试调用此函数myWebView.requestImageRef(msg);



文章来源: How to get link-URL in Android WebView with HitTestResult for a linked image (and not the image-URL) with Longclick