Android中的webview支持页面中的文件上传实例代码
程序员文章站
2023-12-14 21:09:16
android webview在默认情况下是不支持网页中的文件上传功能的;
如果在网页中有,在android web...
android webview在默认情况下是不支持网页中的文件上传功能的;
如果在网页中有<input type="file" />,在android webview中访问时也会出现浏览文件的按钮
但是点击按钮之后没有反应...
那么如何能够让android的webview能够响应,这个浏览按钮呢?在网上查了很多资料,很多相同的,但都漏掉了一个地方,导致无法读取到文件的完整地址(“c:\upfile\233232.jpg”),整理最终代码入下:
我们需要为webview设置webchromeclient,在webchromeclient的实现类中覆盖文件选择的方法:
package com.example.webviewupfile; import java.io.file; import java.io.ioexception; import android.app.activity; import android.content.contentresolver; import android.content.intent; import android.content.res.configuration; import android.database.cursor; import android.graphics.bitmap; import android.net.uri; import android.os.bundle; import android.provider.mediastore; import android.util.log; import android.view.view; import android.webkit.valuecallback; import android.webkit.webchromeclient; import android.webkit.webview; import android.webkit.webviewclient; import android.widget.progressbar; public class mainactivity extends activity { private valuecallback<uri> muploadmessage; private final static int filechooser_resultcode = 1; private webview web; private progressbar progressbar; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); web = (webview) findviewbyid(r.id.webview1); progressbar = (progressbar) findviewbyid(r.id.progressbar1); web = new webview(this); web.getsettings().setjavascriptenabled(true); web.loadurl("http://ueditor.baidu.com/website/onlinedemo.html"); web.setwebviewclient(new mywebclient()); web.setwebchromeclient(new webchromeclient() { // the undocumented magic method override // eclipse will swear at you if you try to put @override here // for android 3.0+ public void openfilechooser(valuecallback<uri> uploadmsg) { muploadmessage = uploadmsg; intent i = new intent(intent.action_get_content); i.addcategory(intent.category_openable); i.settype("image/*"); mainactivity.this.startactivityforresult( intent.createchooser(i, "file chooser"), filechooser_resultcode); } // for android 3.0+ public void openfilechooser(valuecallback uploadmsg, string accepttype) { muploadmessage = uploadmsg; intent i = new intent(intent.action_get_content); i.addcategory(intent.category_openable); i.settype("*/*"); mainactivity.this.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 i = new intent(intent.action_get_content); i.addcategory(intent.category_openable); i.settype("image/*"); mainactivity.this.startactivityforresult( intent.createchooser(i, "file chooser"), mainactivity.filechooser_resultcode); } }); setcontentview(web); } @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; bitmap bm = null; //外界的程序访问contentprovider所提供数据 可以通过contentresolver接口 contentresolver resolver = getcontentresolver(); try { uri originaluri = intent.getdata(); // 获得图片的uri bm = mediastore.images.media.getbitmap(resolver, originaluri); // 这里开始的第二部分,获取图片的路径: string[] proj = { mediastore.images.media.data }; // 好像是android多媒体数据库的封装接口,具体的看android文档 cursor cursor = managedquery(originaluri, proj, null, null, null); // 按我个人理解 这个是获得用户选择的图片的索引值 int column_index = cursor .getcolumnindexorthrow(mediastore.images.media.data); // 将光标移至开头 ,这个很重要,不小心很容易引起越界 cursor.movetofirst(); // 最后根据索引值获取图片路径 string path = cursor.getstring(column_index); uri uri = uri.fromfile(new file(path)); muploadmessage.onreceivevalue(uri); } catch (ioexception e) { log.e("tag-->error", e.tostring()); } } } public class mywebclient extends webviewclient { @override public void onpagestarted(webview view, string url, bitmap favicon) { // todo auto-generated method stub super.onpagestarted(view, url, favicon); } @override public boolean shouldoverrideurlloading(webview view, string url) { // todo auto-generated method stub view.loadurl(url); return true; } @override public void onpagefinished(webview view, string url) { // todo auto-generated method stub super.onpagefinished(view, url); progressbar.setvisibility(view.gone); } } // flipscreen not loading again @override public void onconfigurationchanged(configuration newconfig) { super.onconfigurationchanged(newconfig); } // to handle "back" key press event for webview to go back to previous // screen. /* * @override public boolean onkeydown(int keycode, keyevent event) { if * ((keycode == keyevent.keycode_back) && web.cangoback()) { web.goback(); * return true; } return super.onkeydown(keycode, event); } */ }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
推荐阅读
-
Android中的webview支持页面中的文件上传实例代码
-
Android中复制图片的实例代码
-
Android 中NumberPicker,DatePicker与DatePickerDialog中分割颜色的修改实例代码
-
Android开发中通过手机号+短信验证码登录的实例代码
-
Android中EditText禁止输入表情的实例代码
-
Android中发送Http请求(包括文件上传、servlet接收)的实例代码
-
Android中的Button自定义点击效果实例代码
-
Android中的webview支持页面中的文件上传实例代码
-
Android中复制图片的实例代码
-
Android中如何优雅的处理重复点击实例代码