Let Android WebView support the uploading of files inside HTML

Source: Internet
Author: User
Tags baseuri

By default, Android WebView is not supported <input type=file>, click No response, if you want to click Upload, pop-up Select file, Picture window, then you need to customize a webchromeclient

 Public classMychromeclient extends Webchromeclient { PublicValuecallback<uri>uploadmsg;  PublicValuecallback<uri[]>UPLOADMSG2; PrivateActivity activity;  Public StaticFinalintFilechooser_resultcode =5173;  Public StaticString Mcamerafilepath =""; @SuppressWarnings ("Deprecation")     Publicmychromeclient (Activity Cordova) { This. Activity =Cordova; } @Override Public voidOnprogresschanged (WebView view,intnewprogress)    {super.onprogresschanged (view, newprogress); }            //<input type= "file" Name= "Filefield" id= "Filefield"/>//Android > 4.1.1@Override Publicboolean onshowfilechooser (WebView WebView, Valuecallback<Uri[]>Filepathcallback, Filechooserparams filechooserparams) {        //TODO Auto-generated method stubsUPLOADMSG2 =Filepathcallback;  This. Activity.startactivityforresult (Createdefaultopenableintent (), This.        Filechooser_resultcode); return false; } @SuppressWarnings ("static-access")     Public voidOpenfilechooser (valuecallback<uri>uploadmsg, String accepttype, string capture) {uploadmsg=uploadmsg;  This. Activity.startactivityforresult (Createdefaultopenableintent (), This.    Filechooser_resultcode); }    //3.0 +@SuppressWarnings ("static-access")     Public voidOpenfilechooser (valuecallback<uri>uploadmsg, String accepttype) {uploadmsg=uploadmsg;  This. Activity.startactivityforresult (Createdefaultopenableintent (), This.    Filechooser_resultcode); }    //Android < 3.0@SuppressWarnings ("static-access")     Public voidOpenfilechooser (valuecallback<uri>uploadmsg) {uploadmsg=uploadmsg;  This. Activity.startactivityforresult (Createdefaultopenableintent (), This.    Filechooser_resultcode); }    PrivateIntent createdefaultopenableintent () {Intent I=NewIntent (intent.action_get_content);        I.addcategory (intent.category_openable); I.setdataandtype (MediaStore.Images.Media.EXTERNAL_CONTENT_URI,"image/*"); Intent Chooser= Createchooserintent (Createcameraintent ()/*                                                                 *                                                                  * ,                                                                 *                                                                  * Createcamcorderintent *                                                                 * (),                                                                 * * Createsoundrecorderintent                                                                 *                                                                  * () */);        Chooser.putextra (intent.extra_intent, i); returnChooser; }    PrivateIntent createchooserintent (Intent ... intents) {Intent chooser=NewIntent (Intent.action_chooser);        Chooser.putextra (intent.extra_initial_intents, INTENTS); Chooser.putextra (Intent.extra_title,"Select Picture"); returnChooser; } @SuppressWarnings ("static-access")    PrivateIntent createcameraintent () {Intent cameraintent=NewIntent (mediastore.action_image_capture); File Externaldatadir=environment. Getexternalstoragepublicdirectory (ENVIRONMENT.DIRECTORY_DCIM); File Cameradatadir=NewFile (Externaldatadir.getabsolutepath ()+ File.separator +"515aaa");        Cameradatadir.mkdirs (); String Mcamerafilepath=Cameradatadir.getabsolutepath ()+ File.separator + system.currenttimemillis () +". jpg";  This. Mcamerafilepath =Mcamerafilepath; Cameraintent.putextra (MediaStore.Images.Media.ORIENTATION,0); Cameraintent.putextra (Mediastore.extra_output, Uri.fromfile (NewFile (Mcamerafilepath)); returncameraintent; }    /** Private Intent createcamcorderintent () {return new * * Intent (mediastore.action_video_capture) ; } * * * * private Intent createsoundrecorderintent () {return new * * Intent (Mediastore.audio. Media.record_sound_action); }     */     Public StaticUri Getimagecontenturi (context context, Java.io.File ImageFile) {String FilePath=Imagefile.getabsolutepath (); Cursor Cursor=context.getcontentresolver (). Query (MediaStore.Images.Media.EXTERNAL_CONTENT_URI,Newstring[] {mediastore.images.media._id}, MediaStore.Images.Media.DATA+"=? ",        NewString[] {FilePath},NULL); if(Cursor! =NULL&&Cursor.movetofirst ()) {            intID =cursor.getint (cursor. Getcolumnindex (mediastore.mediacolumns._id)); Uri BaseUri= Uri.parse ("Content://media/external/images/media"); returnUri.withappendedpath (BaseUri,""+ID); } Else {            if(Imagefile.exists ()) {Contentvalues values=Newcontentvalues ();                Values.put (MediaStore.Images.Media.DATA, FilePath); returncontext.getcontentresolver (). Insert (MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); } Else {                return NULL; }        }    }}

Then specify webview to use this client

New Mychromeclient (this));

Let Android WebView support the uploading of files inside 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.