Several ideas of WebView uploading files in Android application development

Source: Internet
Author: User

1. General method, overriding the Openfilechooser method of Webchromeclient

Private classMywebchromeclientextendswebchromeclient {//For Android 3.0+         Public voidOpenfilechooser (valuecallback<uri>uploadmsg, String accepttype) {                 if(Muploadmessage! =NULL)return; Muploadmessage=uploadmsg; Intent I=NewIntent (intent.action_get_content);               I.addcategory (intent.category_openable); I.settype ("*/*"); Startactivityforresult (Intent.createchooser (i,"File Chooser"), 1 ); }            //For Android < 3.0         Public voidOpenfilechooser (valuecallback<uri>uploadmsg) {Openfilechooser (uploadmsg,"" ); }     //For Android > 4.1.1         Public voidOpenfilechooser (valuecallback<uri>uploadmsg, String accepttype, string capture)              {Openfilechooser (uploadmsg, Accepttype); }                        }

This is commonly used, but this openfilechooser is not a public method, android4.4 do not support, swollen? Judge version, if it is 4.4 to call the system browser is a workaround, to the time can be used, but customers do not recognize, only their own implementation of the app upload.

2. Upload your own implementation

PrivateString post (String pathtoourfile)throwsclientprotocolexception, IOException, jsonexception {HttpClient HttpClient=Newdefaulthttpclient (); //set up communication protocol versionshttpclient.getparams (). Setparameter (Coreprotocolpnames.protocol_version, httpversion.http_1_1); HttpPost HttpPost=NewHttpPost (Uploadurl); File File=NewFile (Pathtoourfile); Multipartentity mpentity=NewMultipartentity ();//File TransferContentbody Cbfile =Newfilebody (file); Mpentity.addpart ("UserFile", cbfile);//<input type= "file" name= "UserFile"/> corresponds to thehttppost.setentity (mpentity); HttpResponse Response=Httpclient.execute (HttpPost); Httpentity resentity=response.getentity (); String JSON=""; String Path=""; if(Resentity! =NULL) {JSON=entityutils.tostring (resentity, "Utf-8"); Jsonobject P=NULL; Try{p=NewJsonobject (JSON); Path= (String) p.get ("Path"); }Catch(Exception e) {e.printstacktrace (); }          }          if(Resentity! =NULL) {resentity.consumecontent ();          } httpclient.getconnectionmanager (). Shutdown (); returnpath; }

Use of the Httpmine package, online download.

How do I call it?

Browser.addjavascriptinterface (This, "Android"); Browser is the WebView object.

On the page <a href= "Javascrijpt:window.android.uploadFile ()" > Upload file </a>

UploadFile () is a method of the called activity, which essentially opens the selection dialog, the same process as the first method. Just return the file path and upload it yourself.

The new version of Android system is ready, and there is a problem: in android2.3 and other versions have a bug, the method from JS call app will crash, incompatible, we can only change a method to call this UploadFile

In addition, it is best to choose version 2.2, do not choose 2.3

3. Filtering URLs, overriding Webviewclient's shouldoverrideurlloading method

Private classMywebviewclientextendswebviewclient{PrivateContext Mcontext;  Publicmywebviewclient (Context context) {Super(); Mcontext=context; }                     Public Booleanshouldoverrideurlloading (WebView view, String URL) {if(Url.equalsignorecase ("App:upload") {mainactivity. This. UploadFile (); return true;              } view.loadurl (URL); return true; }         }  

Page <a href= "app:upload" > Uploading Files </a>

Android compatibility issues are really annoying

Several ideas of WebView uploading files in Android application development

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.