Android webview for images, file uploads and start-up camera functions

Source: Internet
Author: User

Directly on the code, realize the WebView powerful function.

Webview.setwebchromeclient (webchromeclient);

Private String Mcamerafilepath = null;private valuecallback<uri> muploadmessage;//form data information private final static int Filechooser_resultcode = result callback for 1;//form </span>
<span style= "FONT-SIZE:18PX;" >private webchromeclient webchromeclient = new Webchromeclient () {//for Android 3.0+public void Openfilechooser (Value Callback<uri> uploadmsg) {if (uploadmsg = = null) {return;} Muploadmessage = uploadmsg;intent i = new Intent (intent.action_get_content); i.addcategory (intent.category_openable); I.settype ("image/*"); Activity.startactivityforresult (Intent.createchooser (i, "File Chooser"), Filechooser_ ResultCode);} For Android 3.0+public void Openfilechooser (Valuecallback uploadmsg, String accepttype) {if (uploadmsg = = null) {return ;} Muploadmessage = uploadmsg;intent i = new Intent (intent.action_get_content); i.addcategory (intent.category_openable); I.settype ("*/*"); Activity.startactivityforresult (Intent.createchooser (i, "File Browser"), Filechooser_resultcode) ;} For Android 4.1public void Openfilechooser (valuecallback<uri> uploadmsg,string accepttype, String capture) {if ( Uploadmsg = = null) {return;} Muploadmessage = uploadmsg;intent i = new InteNT (intent.action_get_content); i.addcategory (intent.category_openable); I.settype ("image/*"); Activity.startactivityforresult (Intent.createchooser (i, "File Chooser"), Filechooser_resultcode);}  Private Intent createdefaultopenableintent () {//Create and return a chooser with the default openable//actions including The camera, camcorder and sound//recorder where available. Intent i = new Intent (intent.action_get_content); i.addcategory (intent.category_openable); I.settype ("*/*"); Intent Chooser = Createchooserintent (Createcameraintent (), Createcamcorderintent (), createsoundrecorderintent ()); Chooser.putextra (intent.extra_intent, i); return chooser;} Private Intent createchooserintent (Intent ... intents) {Intent chooser = new Intent (intent.action_chooser); Chooser.putextra (intent.extra_initial_intents, INTENTS); Chooser.putextra (Intent.extra_title, "File chooser"); Return chooser;} Private Intent createcameraintent () {Intent cameraintent = new Intent (mediastore.action_image_capture); File Externaldatadir= Environment.getexternalstoragepublicdirectory (ENVIRONMENT.DIRECTORY_DCIM); System.out.println ("Externaldatadir:" + externaldatadir); File Cameradatadir = new file (Externaldatadir.getabsolutepath () + File.separator + "Browser-photo"); Cameradatadir.mkdirs (); mcamerafilepath = Cameradatadir.getabsolutepath () + file.separator+ System.currentTimeMillis () + ". jpg"; System.out.println ("Mcamerafilepath:" + Mcamerafilepath); Cameraintent.putextra (Mediastore.extra_output, Uri.fromfile (New File (Mcamerafilepath))); return cameraintent;} Private Intent createcamcorderintent () {return new Intent (mediastore.action_video_capture);} Private Intent createsoundrecorderintent () {return new Intent (MediaStore.Audio.Media.RECORD_SOUND_ACTION);};


public void Onactivityresult (int requestcode, int resultcode, Intent Intent) {if (Requestcode = = Filechooser_resultcode) { if (null = = Muploadmessage) return; Uri result = Intent = = NULL | | ResultCode! = ACTIVITY.RESULT_OK? Null:intent.getData (); if (result = = NULL && Intent = = null&& ResultCode = = ACTIVITY.RESULT_OK) {File came Rafile = new File (Mcamerafilepath), if (Camerafile.exists ()) {result = Uri.fromfile (camerafile); activity.sendbroadcast (New Intent (intent.action_media_scanner_scan_file, result));}} Muploadmessage.onreceivevalue (result); muploadmessage = null;} }


Android webview for images, file uploads and start-up camera functions

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.