android webview上传文件
来源:互联网 发布:外国电视台直播软件 编辑:程序博客网 时间:2024/05/11 11:32
android webview在默认情况下是不支持网页中的文件上传功能的;
如果在网页中有<input type="file" />,在android webview中访问时也会出现浏览文件的按钮
但是点击按钮之后没有反应...
那么如何能够让android的webview能够响应,这个浏览按钮呢?
我们需要为webview设置WebChromeClient,在WebChromeClient的实现类中覆盖文件选择的方法:
01
/***************** android中使用WebView来打开本机的文件选择器 *************************/
02
// js上传文件的<input type="file" name="fileField" id="fileField" />事件捕获
03
// Android > 4.1.1 调用这个方法
04
public
void
openFileChooser(ValueCallback<Uri> uploadMsg,
05
String acceptType, String capture) {
06
mUploadMessage = uploadMsg;
07
Intent intent =
new
Intent(Intent.ACTION_GET_CONTENT);
08
intent.addCategory(Intent.CATEGORY_OPENABLE);
09
intent.setType(
"image/*"
);
10
context.startActivityForResult(
11
Intent.createChooser(intent,
"完成操作需要使用"
),
12
WebMainActivity.FILECHOOSER_RESULTCODE);
13
14
}
15
16
// 3.0 + 调用这个方法
17
public
void
openFileChooser(ValueCallback<Uri> uploadMsg,
18
String acceptType) {
19
mUploadMessage = uploadMsg;
20
Intent intent =
new
Intent(Intent.ACTION_GET_CONTENT);
21
intent.addCategory(Intent.CATEGORY_OPENABLE);
22
intent.setType(
"image/*"
);
23
context.startActivityForResult(
24
Intent.createChooser(intent,
"完成操作需要使用"
),
25
WebMainActivity.FILECHOOSER_RESULTCODE);
26
}
27
28
// Android < 3.0 调用这个方法
29
public
void
openFileChooser(ValueCallback<Uri> uploadMsg) {
30
mUploadMessage = uploadMsg;
31
Intent intent =
new
Intent(Intent.ACTION_GET_CONTENT);
32
intent.addCategory(Intent.CATEGORY_OPENABLE);
33
intent.setType(
"image/*"
);
34
context.startActivityForResult(
35
Intent.createChooser(intent,
"完成操作需要使用"
),
36
WebMainActivity.FILECHOOSER_RESULTCODE);
37
38
}
39
/************** end ***************/
在WebChromeClient的实现类中对应的文件选择事件响应的方法里,使用webview所在的activity对象开启一个android文件选择器,
使用startActivityForResult方法,在开启的文件选择activity结束后需要返回一个值;在webview所在的activity中通过覆盖Activity的onActivityResult方法,得到所需的结果,即选择文件的url
01
* 返回文件选择
02
*/
03
@Override
04
protected
void
onActivityResult(
int
requestCode,
int
resultCode,
05
Intent intent) {
06
if
(requestCode == FILECHOOSER_RESULTCODE) {
07
mUploadMessage = wcci.getmUploadMessage();
08
if
(
null
== mUploadMessage)
09
return
;
10
Uri result = intent ==
null
|| resultCode != RESULT_OK ?
null
11
: intent.getData();
12
mUploadMessage.onReceiveValue(result);
13
mUploadMessage =
null
;
14
15
}
16
}
- android webview上传文件
- Android WebView 文件上传
- android webview上传文件
- android 支持WebView上传文件
- android WebView上传文件代码
- Android WebView上传文件(转)
- Android的webView支持文件上传
- android webview 嵌入html5 定位 文件上传
- Android WebView关于图片/文件上传
- Android webview支持页面中的文件上传
- android使用webview上传文件(图片)
- android WebView的网页上传文件
- Android WebView那些坑之上传文件
- Android WebView那些坑之上传文件
- Android WebView 上传文件支持全解析
- Android WebView 上传图片文件有坑
- android webview无法上传文件的解决方案
- android webview 弹出图片选择器上传文件
- fcntl使用
- Objective-C学习笔记 循环结构
- 每天的运营如何规划?
- C语言深度剖析读书笔记
- C语言之字符串
- android webview上传文件
- 关于内存的一小点总结
- 阿里巴巴2014校招笔试题-2013年9月14日
- EINTR error
- 什么是SEO?SEO能给我们带来什么价值?
- Git基础 3 ---- Git 初始化及仓库创建
- git入门
- EditPlus编辑器也能格式化Java代码
- Matlab编程实现图像增强与去噪