让Android中的webview支持页面中的文件上传
来源:互联网 发布:装饰报价软件 编辑:程序博客网 时间:2024/06/07 08:11
android webview在默认情况下是不支持网页中的文件上传功能的;
如果在网页中有<input type="file" />,在android webview中访问时也会出现浏览文件的按钮
但是点击按钮之后没有反应...
那么如何能够让android的webview能够响应,这个浏览按钮呢?
我们需要为webview设置WebChromeClient,在WebChromeClient的实现类中覆盖文件选择的方法:
/***************** android中使用WebView来打开本机的文件选择器 *************************/// js上传文件的<input type="file" name="fileField" id="fileField" />事件捕获// Android > 4.1.1 调用这个方法public void openFileChooser(ValueCallback<Uri> uploadMsg,String acceptType, String capture) {mUploadMessage = uploadMsg;Intent intent = new Intent(Intent.ACTION_GET_CONTENT);intent.addCategory(Intent.CATEGORY_OPENABLE);intent.setType("image/*");context.startActivityForResult(Intent.createChooser(intent, "完成操作需要使用"),WebMainActivity.FILECHOOSER_RESULTCODE);}// 3.0 + 调用这个方法public void openFileChooser(ValueCallback<Uri> uploadMsg,String acceptType) {mUploadMessage = uploadMsg;Intent intent = new Intent(Intent.ACTION_GET_CONTENT);intent.addCategory(Intent.CATEGORY_OPENABLE);intent.setType("image/*");context.startActivityForResult(Intent.createChooser(intent, "完成操作需要使用"),WebMainActivity.FILECHOOSER_RESULTCODE);}// Android < 3.0 调用这个方法public void openFileChooser(ValueCallback<Uri> uploadMsg) {mUploadMessage = uploadMsg;Intent intent = new Intent(Intent.ACTION_GET_CONTENT);intent.addCategory(Intent.CATEGORY_OPENABLE);intent.setType("image/*");context.startActivityForResult(Intent.createChooser(intent, "完成操作需要使用"),WebMainActivity.FILECHOOSER_RESULTCODE);}/************** end ***************/
在设置WebChromeClient时,要传入一个ValueCallback<Uri> mUploadMessage,同时传入webview所在的Activity的对象;
在WebChromeClient的实现类中对应的文件选择事件响应的方法里,使用webview所在的activity对象开启一个android文件选择器,
使用startActivityForResult方法,在开启的文件选择activity结束后需要返回一个值;在webview所在的activity中通过覆盖Activity的onActivityResult方法,得到所需的结果,即选择文件的url
/** * 返回文件选择 */@Overrideprotected void onActivityResult(int requestCode, int resultCode,Intent intent) {if (requestCode == FILECHOOSER_RESULTCODE) {mUploadMessage = mWebChromeClient.getmUploadMessage();if (null == mUploadMessage)return;Uri result = intent == null || resultCode != RESULT_OK ? null: intent.getData();mUploadMessage.onReceiveValue(result);mUploadMessage = null;}}完成上述操作后就成功的实现了android webview支持文件上传。。。
- 让Android中的webview支持页面中的文件上传
- Android webview支持页面中的文件上传
- 如何是Android中的webView支持页面中的文件上传?
- Android中的webview支持页面中的文件上传
- android 支持WebView上传文件
- Android的webView支持文件上传
- Android WebView 上传文件支持全解析
- Android:让WebView支持<input type="file"…>元素,实现文件上传
- android项目从零开始 Android webview支持 文件上传、图片上传
- android 让 webView 中的超链接失效
- Android中的文件上传
- android使用webview上传文件(支持相册和拍照)
- android使用webview上传文件(支持相册和拍照)
- android使用webview上传文件(支持相册和拍照)
- android使用webview上传文件(支持相册和拍照)
- Android:使用webview上传文件(支持相册和拍照) .
- Android WebView支持Input type=file文件上传
- android使用webview上传文件(支持相册和拍照)
- 把文件中大量的数据去掉重复的再写入文件
- Guava学习笔记:复写的Object常用方法
- ....
- android开发中ListView与Adapter使用要点介绍
- 直线生成算法---简单DDA
- 让Android中的webview支持页面中的文件上传
- mini2440的触摸屏驱动----使用input子系统实现
- 16-素数(算法)
- AOP和IOC的理解
- Django 最佳实践
- C语言函数sscanf()的用法
- [Linux] 刚学习shell,碰到问题.执行时总出错:unexpected operator(转载)
- Mysql全文索引
- C++五大内存分区