Android: Let WebView support <input type= "file" ...> element

Source: Internet
Author: User

Recently in an activity page: Users upload a picture to zoom, rotate and click Next to fill in the content to create a picture!

After a good after a variety of testing is no problem, there is no obvious bug, the process can walk through, but embedded in the app, the problem comes!

Basically on iOS, there are obvious problems on Android, and here's what I'm going to say:

In Android, when we open a page through WebView, if there are elements <input type= "file" ...> type, WebView only normal display style, but is not clickable. To solve this problem, we need to rewrite webchromeclient.

The demo code is given directly below:

Activity file:

 Public classMainactivity extends Activity {PrivateFinal String host ="demo.com"; PrivateFinal String urladdress ="/ http"+host; PrivateWebView web; PrivateProgressBar ProgressBar; PrivateValuecallback<uri>Muploadmessage; PrivateFinalStatic intFilechooser_resultcode =1; @Overrideprotected voidOnactivityresult (intRequestcode,intResultCode, Intent Intent) {        if(Requestcode = =Filechooser_resultcode) {            if(NULL= = Muploadmessage)return; Uri result= Intent = =NULL|| ResultCode! = RESULT_OK?NULL: Intent.getdata ();            Muploadmessage.onreceivevalue (result); Muploadmessage=NULL; }    }    /** * Called when the activity is first created. */@SuppressLint ("setjavascriptenabled") @Overrideprotected voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.main); Web=(WebView) Findviewbyid (R.ID.WEBVIEW1); ProgressBar=(ProgressBar) Findviewbyid (R.ID.PROGRESSBAR1); WebSettings Settings=web.getsettings (); Settings.setjavascriptenabled (true);        Web.loadurl (urladdress); Web.setwebviewclient (Newmywebviewclient ()); Web.setwebchromeclient (Newwebchromeclient () {//The key code, the following function is no API documentation, so in eclipse will be an error, if added @override keyword here. //For Android 3.0+             Public voidOpenfilechooser (valuecallback<uri>uploadmsg) {Muploadmessage=uploadmsg; Intent I=NewIntent (intent.action_get_content);                I.addcategory (intent.category_openable); I.settype ("image/*"); Mainactivity. This. Startactivityforresult (Intent.createchooser (i,"File Chooser"), Filechooser_resultcode); }            //For Android 3.0+             Public voidOpenfilechooser (Valuecallback uploadmsg, String accepttype) {muploadmessage=uploadmsg; Intent I=NewIntent (intent.action_get_content);                I.addcategory (intent.category_openable); I.settype ("*/*"); Mainactivity. This. Startactivityforresult (Intent.createchooser (i,"File Browser"), Filechooser_resultcode); }            //For Android 4.1             Public voidOpenfilechooser (valuecallback<uri>uploadmsg, String accepttype, string capture) {Muploadmessage=uploadmsg; Intent I=NewIntent (intent.action_get_content);                I.addcategory (intent.category_openable); I.settype ("image/*"); Mainactivity. This. Startactivityforresult (Intent.createchooser (i,"File Chooser"), Mainactivity.filechooser_resultcode); }        });//Setcontentview (web);    }    Private classMywebviewclient extends Webviewclient {@Override PublicBoolean shouldoverrideurlloading (WebView view, String URL) {if(Uri.parse (URL). GetHost (). Equals (host)) {//This is my web site and so does not override; Let my WebView load the page                return false; }            //Otherwise, the link isn't for a page in my site, so launch another Activity that handles URLsIntent Intent =NewIntent (Intent.action_view, Uri.parse (URL));            StartActivity (Intent); return true; } @Override Public voidonpagestarted (WebView view, String URL, Bitmap favicon) {//TODO auto-generated Method Stubsuper.onpagestarted (view, URL, favicon); } @Override Public voidonpagefinished (WebView view, String URL) {//TODO auto-generated Method Stubsuper.onpagefinished (view, URL);        Progressbar.setvisibility (View.gone); }    }    //Flipscreen not loading again@Override Public voidonconfigurationchanged (Configuration newconfig) {super.onconfigurationchanged (newconfig); }    //Capture the rewind button so that WebView can fall back to the previous page instead of shutting down the activity directly. @Override PublicBoolean OnKeyDown (intKeyCode, KeyEventEvent) {        if((keycode = = Keyevent.keycode_back) &&Web.cangoback ())            {Web.goback (); return true; }        returnSuper.onkeydown (KeyCode,Event); }}

Layout code is not posted out, is very simple a webview and a progress. With the above code, we are able to upload files in the webview.

PS: Compared with iOS, this is not a perfect solution, because it does not support direct photo upload files, if you need this feature, then you need to do further development.

Original address: Android: Let WebView support input:file elements

Android: Let WebView support <input type= "file" ...> element

Related Article

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.