Android webview: How to allow file inputs in Android webview fragement class? -
android webview: how allow file inputs in android webview fragement class? default webview class not supporting file inputs
here answer googlechrome github:
public class mainfragment extends fragment { private static final string tag = mainfragment.class.getsimplename(); public static final int input_file_request_code = 1; public static final string extra_from_notification = "extra_from_notification"; private webview mwebview; private valuecallback<uri[]> mfilepathcallback; private string mcameraphotopath; public mainfragment() { } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.fragment_main, container, false); // reference of webview layout/activity_main.xml mwebview = (webview) rootview.findviewbyid(r.id.fragment_main_webview); setupwebviewdefaults(mwebview); // check whether we're recreating destroyed instance if (savedinstancestate != null) { // restore previous url , history stack mwebview.restorestate(savedinstancestate); } mwebview.setwebchromeclient(new webchromeclient() { public boolean onshowfilechooser( webview webview, valuecallback<uri[]> filepathcallback, webchromeclient.filechooserparams filechooserparams) { if(mfilepathcallback != null) { mfilepathcallback.onreceivevalue(null); } mfilepathcallback = filepathcallback; intent takepictureintent = new intent(mediastore.action_image_capture); if (takepictureintent.resolveactivity(getactivity().getpackagemanager()) != null) { // create file photo should go file photofile = null; try { photofile = createimagefile(); takepictureintent.putextra("photopath", mcameraphotopath); } catch (ioexception ex) { // error occurred while creating file log.e(tag, "unable create image file", ex); } // continue if file created if (photofile != null) { mcameraphotopath = "file:" + photofile.getabsolutepath(); takepictureintent.putextra(mediastore.extra_output, uri.fromfile(photofile)); } else { takepictureintent = null; } } intent contentselectionintent = new intent(intent.action_get_content); contentselectionintent.addcategory(intent.category_openable); contentselectionintent.settype("image/*"); intent[] intentarray; if(takepictureintent != null) { intentarray = new intent[]{takepictureintent}; } else { intentarray = new intent[0]; } intent chooserintent = new intent(intent.action_chooser); chooserintent.putextra(intent.extra_intent, contentselectionintent); chooserintent.putextra(intent.extra_title, "image chooser"); chooserintent.putextra(intent.extra_initial_intents, intentarray); startactivityforresult(chooserintent, input_file_request_code); return true; } }); // load local index.html file if(mwebview.geturl() == null) { mwebview.loadurl("file:///android_asset/www/index.html"); } return rootview; } /** * more info method can found @ * http://developer.android.com/training/camera/photobasics.html * * @return * @throws ioexception */ private file createimagefile() throws ioexception { // create image file name string timestamp = new simpledateformat("yyyymmdd_hhmmss").format(new date()); string imagefilename = "jpeg_" + timestamp + "_"; file storagedir = environment.getexternalstoragepublicdirectory( environment.directory_pictures); file imagefile = file.createtempfile( imagefilename, /* prefix */ ".jpg", /* suffix */ storagedir /* directory */ ); return imagefile; } /** * convenience method set generic defaults * given webview * * @param webview */ @targetapi(build.version_codes.honeycomb) private void setupwebviewdefaults(webview webview) { websettings settings = webview.getsettings(); // enable javascript settings.setjavascriptenabled(true); // use wideviewport , zoom out if there no viewport defined settings.setusewideviewport(true); settings.setloadwithoverviewmode(true); // enable pinch zoom without zoom buttons settings.setbuiltinzoomcontrols(true); if(build.version.sdk_int > build.version_codes.honeycomb) { // hide zoom controls honeycomb+ settings.setdisplayzoomcontrols(false); } // enable remote debugging via chrome://inspect if(build.version.sdk_int >= build.version_codes.kitkat) { webview.setwebcontentsdebuggingenabled(true); } // set webviewclient ensure links consumed webview rather // passed browser if can mwebview.setwebviewclient(new webviewclient()); } @override public void onactivityresult (int requestcode, int resultcode, intent data) { if(requestcode != input_file_request_code || mfilepathcallback == null) { super.onactivityresult(requestcode, resultcode, data); return; } uri[] results = null; // check response 1 if(resultcode == activity.result_ok) { if(data == null) { // if there not data, may have taken photo if(mcameraphotopath != null) { results = new uri[]{uri.parse(mcameraphotopath)}; } } else { string datastring = data.getdatastring(); if (datastring != null) { results = new uri[]{uri.parse(datastring)}; } } } mfilepathcallback.onreceivevalue(results); mfilepathcallback = null; return; }
Comments
Post a Comment