Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I checked the source code of the WebView and it seems that the image uri is the only extra data you can get for SRC_IMAGE_ANCHOR_TYPE. But don't be mad here I have a quick and dirty workaround for you:</p> <pre><code> 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; } }); </code></pre> <p>I tried the code on a low-end Android 2.1 device and a high-end Android 4.0 device, both work like a charm. </p> <p>Regards</p> <p>Ziteng Chen</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload