WebView does not support file upload <input type= "files"/>

Source: Internet
Author: User
Tags html file upload

The project encountered the need for file uploads, iOS is directly supported, Android but did not respond, some models even flash back.

Find a lot of reasons, in the online search method, measured feasible, do a record, knowledge sharing.

HTML File Upload

<input type= "File" class= "uploadbtn" multiple= "multiple" accept= "Image/jpeg" >

Mobile End WebView Rewrite method

    //1. Set webchromeclient, rewrite file upload callbackMwebview.setwebchromeclient (Newwebchromeclient () {//For Android < 3.0             Public voidOpenfilechooser (valuecallback<uri>valuecallback) {Muploadmessage=Valuecallback;            Openimagechooseractivity (); }            //For Android >= 3.0             Public voidOpenfilechooser (Valuecallback valuecallback, String accepttype) {muploadmessage=Valuecallback;            Openimagechooseractivity (); }            //For Android >= 4.1             Public voidOpenfilechooser (valuecallback<uri>Valuecallback, String accepttype, string capture) {Muploadmessage=Valuecallback;            Openimagechooseractivity (); }            //For Android >= 5.0@Override Public BooleanOnshowfilechooser (WebView WebView, valuecallback<uri[]>Filepathcallback, Webchromeclient.filechooserparams filechooserparams) {Muploadcallbackabovel=Filepathcallback;                Openimagechooseractivity (); return true;        }        }); //2. Callback method triggers local selection file        Private voidopenimagechooseractivity () {Intent I=NewIntent (intent.action_get_content);            I.addcategory (intent.category_openable); I.settype ("Image/*"); Startactivityforresult (Intent.createchooser (i,"Image Chooser"), Filechooser_resultcode); }        //3. Select Picture Post-processing@Overrideprotected voidOnactivityresult (intRequestcode,intResultCode, Intent data) {            Super. Onactivityresult (Requestcode, ResultCode, data); if(Requestcode = =File_chooser_result_code) {                if(NULL= = Uploadmessage &&NULL= = Uploadmessageabovel)return; Uri result= Data = =NULL|| ResultCode! = RESULT_OK?NULL: Data.getdata (); if(Uploadmessageabovel! =NULL) {onactivityresultabovel (Requestcode, ResultCode, data); } Else if(Uploadmessage! =NULL) {uploadmessage.onreceivevalue (result); Uploadmessage=NULL; }            }        }            //4. Select content callback to HTML page@TargetApi (build.version_codes. LOLLIPOP)Private voidOnactivityresultabovel (intRequestcode,intResultCode, Intent Intent) {            if(Requestcode! = File_chooser_result_code | | uploadmessageabovel = =NULL)                return; Uri[] Results=NULL; if(ResultCode = =ACTIVITY.RESULT_OK) {                if(Intent! =NULL) {String datastring=intent.getdatastring (); Clipdata Clipdata=Intent.getclipdata (); if(Clipdata! =NULL) {Results=NewUri[clipdata.getitemcount ()];  for(inti = 0; I < Clipdata.getitemcount (); i++) {Clipdata.item Item=Clipdata.getitemat (i); Results[i]=Item.geturi (); }                    }                    if(Datastring! =NULL) Results=Newuri[]{uri.parse (datastring)};            }} uploadmessageabovel.onreceivevalue (results); Uploadmessageabovel=NULL; }    }

WebView does not support file upload <input type= "files"/>

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.