[Palm Eye] Android WebView Long Press to save pictures to phone

Source: Internet
Author: User

concrete effect visible "palm eye" palm clap antique Lake:http://bbs.guwanch.com

PrivateString Imgurl = ""; /*** * Function: Long press the picture to save to the phone*/@Override Public voidOncreatecontextmenu (ContextMenu menu, View V, contextmenuinfo menuinfo) {Super. Oncreatecontextmenu (menu, V, menuinfo); Menuitem.onmenuitemclicklistener Handler=NewMenuitem.onmenuitemclicklistener () { Public BooleanOnmenuitemclick (MenuItem item) {if(Item.gettitle () = = "Save to Phone") {                    NewSaveImage (). Execute ();//Android 4.0 to use threads to access the network later}Else {                    return false; }                return true;        }        }; if(VinstanceofWebView) {Webview.hittestresult result=( (WebView) v). Gethittestresult (); if(Result! =NULL) {                intType =Result.gettype (); if(type = = WebView.HitTestResult.IMAGE_TYPE | | type = =WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE) {Imgurl=Result.getextra (); Menu.setheadertitle (Prompted); Menu.add (0, V.getid (), 0, "Save to Phone"). Setonmenuitemclicklistener (handler); }            }        }    }    /*** * function: Save picture with thread * *@authorWangyp **/    Private classSaveImageextendsAsynctask<string, Void, string>{@Overrideprotectedstring Doinbackground (String ... params) {string result= ""; Try{String SDcard=environment.getexternalstoragedirectory (). toString (); File File=NewFile (SDcard + "/download"); if(!file.exists ())                {File.mkdirs (); }                intIDX = Imgurl.lastindexof ("."); String ext=imgurl.substring (IDX); File=NewFile (SDcard + "/download/" +NewDate (). GetTime () +ext); InputStream InputStream=NULL; URL URL=NewURL (Imgurl); HttpURLConnection Conn=(HttpURLConnection) url.openconnection (); Conn.setrequestmethod ("GET"); Conn.setconnecttimeout (20000); if(Conn.getresponsecode () = = 200) {InputStream=Conn.getinputstream (); }                byte[] buffer =New byte[4096]; intLen = 0; FileOutputStream OutStream=Newfileoutputstream (file);  while(len = inputstream.read (buffer))! =-1) {outstream.write (buffer,0, Len);                } outstream.close (); Result= "Picture saved to:" +File.getabsolutepath (); } Catch(Exception e) {result= "Save failed!" " +E.getlocalizedmessage (); }            returnresult; } @Overrideprotected voidOnPostExecute (String result) {MsgBox (Prompted, result); }    }

Specific effects download App test: or follow the public number:

                   

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.