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 classMainactivityextendsActivity {Private FinalString host = "Demo.com"; Private FinalString urladdress = "http//" +host; PrivateWebView web; PrivateProgressBar ProgressBar; PrivateValuecallback<uri>Muploadmessage; Private Final Static 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 classMywebviewclientextendswebviewclient {@Override Public Booleanshouldoverrideurlloading (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 Stub Super. onpagestarted (view, URL, favicon); } @Override Public voidonpagefinished (WebView view, String URL) {//TODO auto-generated Method Stub Super. 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 Public BooleanOnKeyDown (intKeyCode, KeyEvent event) { if((keycode = = Keyevent.keycode_back) &&Web.cangoback ()) {Web.goback (); return true; } return Super. 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.