Android webview打开本地图片上传实现代码
程序员文章站
2023-12-19 21:32:46
webview打开本地图片选择器十分之麻烦,其在安卓系统3x 4x 5x上的行为都不同,处理也不同,所以之前差点崩溃。经过测试和完善,最终其在各个版本上都能完美工作。...
webview打开本地图片选择器十分之麻烦,其在安卓系统3x 4x 5x上的行为都不同,处理也不同,所以之前差点崩溃。经过测试和完善,最终其在各个版本上都能完美工作。
直接上代码
package com.testandroid.webview; import android.content.intent; import android.net.uri; import android.os.bundle; import android.support.v7.app.alertdialog; import android.support.v7.app.appcompatactivity; import android.view.keyevent; import android.view.view; import android.webkit.jsresult; import android.webkit.valuecallback; import android.webkit.webbackforwardlist; import android.webkit.webchromeclient; import android.webkit.webview; import android.webkit.webviewclient; import android.widget.button; import com.testandroid.r; public class webviewactivity extends appcompatactivity { private final string tag = webviewactivity.class.getsimplename(); private button button; private webview webview; private string recgpic = "http://m.shitu.chinaso.com/mx/index.html"; public final static int filechooser_resultcode = 1; public final static int filechooser_resultcode_for_android_5 = 2; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_web_view); button = (button) findviewbyid(r.id.button); button.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { } }); inittestwebview(); } private void inittestwebview() { webview = (webview) findviewbyid(r.id.tempwebview); wiewsettings settings = webview.getsettings(); settings.setjavascriptenabled(true); webview.setwebchromeclient(new webchromeclient() { @override public boolean onjsalert(webview view, string url, string message, jsresult result) { alertdialog.builder builder = new alertdialog.builder(view.getcontext()); builder.settitle("xxx提示").setmessage(message).setpositivebutton("确定", null); builder.setcancelable(false); builder.seticon(r.mipmap.ic_launcher); alertdialog dialog = builder.create(); dialog.show(); result.confirm(); return true; } //扩展浏览器上传文件 //3.0++版本 public void openfilechooser(valuecallback<uri> uploadmsg, string accepttype) { openfilechooserimpl(uploadmsg); } //3.0--版本 public void openfilechooser(valuecallback<uri> uploadmsg) { openfilechooserimpl(uploadmsg); } public void openfilechooser(valuecallback<uri> uploadmsg, string accepttype, string capture) { openfilechooserimpl(uploadmsg); } @override public boolean onshowfilechooser(webview webview, valuecallback<uri[]> filepathcallback, filechooserparams filechooserparams) { onenfilechooseimpleforandroid(filepathcallback); return true; } }); webview.setwebviewclient(new webviewclient() { @override public boolean shouldoverrideurlloading(webview view, string url) { view.loadurl(url); return true; } }); webview.loadurl(recgpic); } public valuecallback<uri> muploadmessage; private void openfilechooserimpl(valuecallback<uri> uploadmsg) { muploadmessage = uploadmsg; intent i = new intent(intent.action_get_content); i.addcategory(intent.category_openable); i.settype("image/*"); startactivityforresult(intent.createchooser(i, "file chooser"), filechooser_resultcode); } public valuecallback<uri[]> muploadmessageforandroid5; private void onenfilechooseimpleforandroid(valuecallback<uri[]> filepathcallback) { muploadmessageforandroid5 = filepathcallback; intent contentselectionintent = new intent(intent.action_get_content); contentselectionintent.addcategory(intent.category_openable); contentselectionintent.settype("image/*"); intent chooserintent = new intent(intent.action_chooser); chooserintent.putextra(intent.extra_intent, contentselectionintent); chooserintent.putextra(intent.extra_title, "image chooser"); startactivityforresult(chooserintent, filechooser_resultcode_for_android_5); } @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; } else if (requestcode == filechooser_resultcode_for_android_5){ if (null == muploadmessageforandroid5) return; uri result = (intent == null || resultcode != result_ok) ? null: intent.getdata(); if (result != null) { muploadmessageforandroid5.onreceivevalue(new uri[]{result}); } else { muploadmessageforandroid5.onreceivevalue(new uri[]{}); } muploadmessageforandroid5 = null; } } @override public boolean onkeydown(int keycode, keyevent event) { if (webview.cangoback() && event.getkeycode() == keyevent.keycode_back) { //获取历史列表 webbackforwardlist mwebbackforwardlist = webview .copybackforwardlist(); //判断当前历史列表是否最顶端,其实cangoback已经判断过 if (mwebbackforwardlist.getcurrentindex() > 0) { webview.goback(); return true; } } return super.onkeydown(keycode, event); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。