java - upload file in webview android studio -
helo, trying upload file ore picture on 1 bottom in webpage using webview in android studio, can not chose file or picture.... code please, if can great... try did action 2 week, searching in different webpage , can not that..
private webview mwebview; final int select_photo = 1; private string url = "http://xxx..."; private valuecallback<uri> muploadmessage; private final static int filechooser_resultcode=1; @override protected void onactivityresult(int requestcode, int resultcode, intent intent) { if (requestcode == filechooser_resultcode) { if (null == muploadmessage) return; uri result = intent == null || resultcode != result_ok ? null : intent.getdata(); muploadmessage.onreceivevalue(result); muploadmessage = null; } } @override public void onbackpressed() { finish(); } @suppresslint("setjavascriptenabled") protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_mostrar_laravel); mwebview = (webview) findviewbyid(r.id.activity_main_webview_laravel); webview webview = new webview(this); websettings websettings = webview.getsettings(); webview.getsettings().setloadwithoverviewmode(true); webview.getsettings().setjavascriptenabled(true); websettings.setjavascriptenabled(true); websettings.setallowfileaccess(true); websettings.setallowcontentaccess(true); websettings.setloadsimagesautomatically(true); websettings.setsaveformdata(false); websettings.setjavascriptenabled(true); websettings.setsupportzoom(false); webview.setwebchromeclient(new mostrar_laravel.mywebchromeclient()); websettings.setblocknetworkloads(false); webview.setscrollbarstyle(webview.scrollbars_outside_overlay); webview.setscrollbarfadingenabled(false); webview.getsettings().setbuiltinzoomcontrols(true); webview.getsettings().setpluginstate(websettings.pluginstate.on); webview.getsettings().setallowfileaccess(true); webview.getsettings().setsupportzoom(true); webview.addjavascriptinterface(new mostrar_laravel.myjavascriptinterface(this), "android"); webview.getsettings().setbuiltinzoomcontrols(true); setcontentview(webview); final websettings settings = webview.getsettings(); settings.setjavascriptenabled(true); webview.setwebviewclient(new webviewclient()); settings.setappcacheenabled(true); settings.setbuiltinzoomcontrols(true); settings.setpluginstate(websettings.pluginstate.on); webview.setwebchromeclient(new webchromeclient()); cookiesyncmanager.createinstance(mostrar_laravel.this); cookiemanager cookiemanager = cookiemanager.getinstance(); cookiemanager.removesessioncookie(); string cookiestring = "param=value"; cookiemanager.setcookie("xxx...", cookiestring); cookiesyncmanager.getinstance().sync(); map<string, string> abc = new hashmap<string, string>(); abc.put("cookie", cookiestring); webview.setwebchromeclient(new webchromeclient() { public void onprogresschanged(webview view, int progress) { } //the undocumented magic method override //eclipse swear @ if try put @override here // android 3.0+ public void openfilechooser(valuecallback<uri> uploadmsg) { muploadmessage = uploadmsg; intent = new intent(intent.action_get_content); i.addcategory(intent.category_openable); i.settype("image/*"); startactivityforresult(intent.createchooser(i, "file chooser"), filechooser_resultcode); } // android 3.0+ public void openfilechooser(valuecallback uploadmsg, string accepttype) { muploadmessage = uploadmsg; intent = new intent(intent.action_get_content); i.addcategory(intent.category_openable); i.settype("*/*"); startactivityforresult( intent.createchooser(i, "file browser"), filechooser_resultcode); } //for android 4.1 public void openfilechooser(valuecallback<uri> uploadmsg, string accepttype, string capture) { muploadmessage = uploadmsg; intent = new intent(intent.action_get_content); i.addcategory(intent.category_openable); i.settype("image/*"); startactivityforresult(intent.createchooser(i, "file chooser"), filechooser_resultcode); } }); webview.loadurl(url,abc); this.mwebview.setwebviewclient(new webviewclient(){ public void onreceivederror(webview view, int errorcode, string description, string failingurl) { mwebview.loadurl("about:blank"); view.clearhistory(); } @override public boolean shouldoverrideurlloading(webview view, string url){ view.loadurl(url); return true; } }); } final class mywebchromeclient extends webchromeclient { @override public boolean onjsalert(webview view, string url, string message, jsresult result) { result.confirm(); return true; } public void openfilechooser(valuecallback<uri> uploadmsg) { muploadmessage = uploadmsg; intent = new intent(intent.action_get_content); i.addcategory(intent.category_openable); i.settype("image/*"); mostrar_laravel.this.startactivityforresult( intent.createchooser(i, "image browser"), filechooser_resultcode); } } class myjavascriptinterface { context mcontext; myjavascriptinterface(context c) { mcontext = c; } @javascriptinterface public void showtoast(string toast) { toast.maketext(mcontext, toast, toast.length_short).show(); } @javascriptinterface public string choosephoto() { // todo auto-generated method stub string file = "test"; intent photopickerintent = new intent(intent.action_pick); photopickerintent.settype("image/*"); startactivityforresult(photopickerintent, select_photo); return file; } @javascriptinterface public void openandroiddialog() { alertdialog.builder mydialog = new alertdialog.builder(mostrar_laravel.this); mydialog.settitle("danger!"); mydialog.setmessage("you can want!"); mydialog.setpositivebutton("on", null); mydialog.show(); } } public string getrealpathfromuri(context context, uri contenturi) { cursor cursor = null; try { string[] proj = { mediastore.images.media.data }; cursor = context.getcontentresolver().query(contenturi, proj, null, null, null); int column_index = cursor.getcolumnindexorthrow(mediastore.images.media.data); cursor.movetofirst(); return cursor.getstring(column_index); } { if (cursor != null) { cursor.close(); } } }
Comments
Post a Comment