Register context menu for picture in Android:webview

Source: Internet
Author: User

Objective

Today, a friend asked me a question about how to add a context menu to a picture in a WebView control to add features such as saving a picture. Today gave him a simple demo demo, now write down, to the same problem of friends to provide a little thought it.

Overview implementation

In fact, this function is very simple, not too complicated things, is the use of WebView control, one is to WebView register the context menu event, the second is in response to the event to determine the type of event source, if it is a picture type, the URL is taken out

Registering context Menu Events

This is easier to do with the code below.

 

WebView VW =this. Registerforcontextmenu (VW);

Fetch image URL

The point of this section is to respond only to the context menu of the image source and the path to the picture. The implementation code is as follows

  

1 @Override2      Public voidOncreatecontextmenu (ContextMenu menu, View V,3 contextmenuinfo Menuinfo) {4         Super. Oncreatecontextmenu (menu, V, menuinfo);5 6WebView W =(WebView) v;7Hittestresult result =W.gethittestresult ();8         //detect only picture formats9         if(Result.gettype () = =Hittestresult.image_type) {TenMenu.addsubmenu (1, 1, 1, "Save Picture"); One  A             //Remove URL via Result.getextra () -strURL =Result.getextra (); - Toast.maketext (Getapplicationcontext (), strURL, Toast.length_short). Show (); the         } -}

The point is:Gethittestresult, first look at the official note

Gets a hittestresult based on the current cursor node. If a html::img tag is found, the Hittestresult type was set to Image_type and the URL was set in the "Extra" field.

This means: Get a Hittestresult object based on the node that is currently being triggered. If the event is triggered by an IMG tag, then the Hittestresult type is Image_type, and the URL is placed in the extra domain of the hittestresult.

So, by result.gettype () = = Hittestresult.image_type This sentence, you can achieve only the response to the picture event, and Result.getextra (), you can take the image URL. This allows the URL to be further manipulated, such as saving pictures and so on.

In this way, this function is simple to implement.

Postscript

This is a response to the image source of an example, in the same vein, you can respond to hyperlinks, phone, mail and other event sources of processing action, the following Gethittestresult full instructions to paste:

Gets a hittestresult based on the current cursor node. If a html::a tag is found and the anchor have a non-javascript URL, the Hittestresult type is set to Src_anchor_type and th E URL is set in the "Extra" field. If the anchor does not had a URL or if it is a JavaScript URL, the type would be unknown_type and the URL had to be Retrie Ved through Requestfocusnodehref (Message) asynchronously. If a html::img tag is found, the Hittestresult type was set to Image_type and the URL was set in the "Extra" field. A type of Src_image_anchor_type indicates an ANCHOR with a URL, which has a, IMAGE as a child node. If a phone number is found, the Hittestresult type was set to Phone_type and the phone number is set in the "Extra" field O F Hittestresult. If A map address is found, the Hittestresult type was set to Geo_type and the address is set in the "Extra" field of hittes TResult. If an e-mail address is found, the Hittestresult type was set to Email_type and the e-mail is set in the ' Extra ' field of hit TestResult. Otherwise, Hittestresult type is set to Unknown_type.

The approximate meaning is to obtain a Hittestresult object based on the node that is currently being triggered. If the event source is a label and has a URL that is not JavaScript, then the Hittestresult type is set to Src_anchor_type, and the URL is placed in the extra domain. If the a tag does not have a URL or is a JavaScript URL, then the type is set to Unknown_type and the URL is retrieved again. If the event source is an IMG tag, then the Hittestresult type is Image_type, and the URL is placed in the extra domain of the hittestresult. A src_image_anchor_type type indicates a hyperlink that has a picture as a child object. If it is a phone number, then the type will be set to Phone_type, and the phone number is set to the extra domain. If it is a map address, the type is geo_type and the address information is placed in the extra. If it is an e-mail address, then the type is set to Email_type and Emal is set to the extra domain. The other event sources are of type Unknown_type.

Complete code: Https://github.com/xiaoai-opensource/WebViewContextMenu

Original link: http://www.cnblogs.com/luoaz/p/3756160.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.