让Android的WebView支持html里面的文件上传
默认情况下,Android的webview是不支持<input type=file>的,点击没有任何反应,如果希望点击上传,弹出选择文件、图片的窗口,那就需要自定义一个WebChromeClient
public class MyChromeClient extends WebChromeClient {
public ValueCallback<Uri> UploadMsg;
public ValueCallback<Uri[]> UploadMsg2;
private Activity activity;
public static final int FILECHOOSER_RESULTCODE = ;
public static String mCameraFilePath = "";
@SuppressWarnings("deprecation")
public MyChromeClient(Activity cordova) {
this.activity = cordova;
}
@Override
public void onProgressChanged(WebView view, int newProgress) {
super.onProgressChanged(view, newProgress);
}
// <input type="file" name="fileField" id="fileField" />
// Android > 4.1.1
@Override
public boolean onShowFileChooser(WebView webView,
ValueCallback<Uri[]> filePathCallback,
FileChooserParams fileChooserParams) {
// TODO 自动生成的方法存根
UploadMsg2 = filePathCallback;
this.activity.startActivityForResult(createDefaultOpenableIntent(),
this.FILECHOOSER_RESULTCODE);
return false;
}
@SuppressWarnings("static-access")
public void openFileChooser(ValueCallback<Uri> uploadMsg,
String acceptType, String capture) {
UploadMsg = uploadMsg;
this.activity.startActivityForResult(createDefaultOpenableIntent(),
this.FILECHOOSER_RESULTCODE);
}
// 3.0 +
@SuppressWarnings("static-access")
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
UploadMsg = uploadMsg;
this.activity.startActivityForResult(createDefaultOpenableIntent(),
this.FILECHOOSER_RESULTCODE);
}
// Android < 3.0
@SuppressWarnings("static-access")
public void openFileChooser(ValueCallback<Uri> uploadMsg) {
UploadMsg = uploadMsg;
this.activity.startActivityForResult(createDefaultOpenableIntent(),
this.FILECHOOSER_RESULTCODE);
}
private Intent createDefaultOpenableIntent() {
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
"image/*");
Intent chooser = createChooserIntent(createCameraIntent()/*
*
* ,
*
* createCamcorderIntent
*
* (),
*
* createSoundRecorderIntent
*
* ()
*/);
chooser.putExtra(Intent.EXTRA_INTENT, i);
return chooser;
}
private Intent createChooserIntent(Intent... intents) {
Intent chooser = new Intent(Intent.ACTION_CHOOSER);
chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, intents);
chooser.putExtra(Intent.EXTRA_TITLE, "选择图片");
return chooser;
}
@SuppressWarnings("static-access")
private Intent createCameraIntent() {
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File externalDataDir = Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
File cameraDataDir = new File(externalDataDir.getAbsolutePath()
+ File.separator + "515aaa");
cameraDataDir.mkdirs();
String mCameraFilePath = cameraDataDir.getAbsolutePath()
+ File.separator + System.currentTimeMillis() + ".jpg";
this.mCameraFilePath = mCameraFilePath;
cameraIntent.putExtra(MediaStore.Images.Media.ORIENTATION, );
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(new File(mCameraFilePath)));
return cameraIntent;
}
/*
*
* private Intent createCamcorderIntent() { return new
*
* Intent(MediaStore.ACTION_VIDEO_CAPTURE); }
*
*
*
* private Intent createSoundRecorderIntent() { return new
*
* Intent(MediaStore.Audio.Media.RECORD_SOUND_ACTION); }
*/
public static Uri getImageContentUri(Context context, java.io.File imageFile) {
String filePath = imageFile.getAbsolutePath();
Cursor cursor = context.getContentResolver().query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
new String[] { MediaStore.Images.Media._ID },
MediaStore.Images.Media.DATA + "=? ",
new String[] { filePath }, null);
if (cursor != null && cursor.moveToFirst()) {
int id = cursor.getInt(cursor
.getColumnIndex(MediaStore.MediaColumns._ID));
Uri baseUri = Uri.parse("content://media/external/images/media");
return Uri.withAppendedPath(baseUri, "" + id);
} else {
if (imageFile.exists()) {
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.DATA, filePath);
return context.getContentResolver().insert(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
} else {
return null;
}
}
}
}
然后指定webview使用这个client
webview.setWebChromeClient( new MyChromeClient (this));
让Android的WebView支持html里面的文件上传的更多相关文章
- 让Android中的webview支持页面中的文件上传
android webview在默认情况下是不支持网页中的文件上传功能的: 如果在网页中有<input type="file" />,在android webview中 ...
- Android与Asp.Net Web服务器的文件上传下载BUG汇总[更新]
遇到的问题: 1.java.io.IOException: open failed: EINVAL (Invalid argument)异常,在模拟器中的sd卡创建文件夹和文件时报错 出错原因可能是: ...
- android开发里跳过的坑——图片文件上传失败
使用的apache的httpclient的jar包,做的http图片上传,上传时,服务器总返文件格式不对.后来发现,是由于在创建FileBody时,使用了默认的ContentType引起的.所以服务器 ...
- android批量文件上传(android批量图片上传)
项目中多处用到文件批量上传功能,今天正好解决了此问题,在此写出来,以便日后借鉴. 首先,以下架构下的批量文件上传可能会失败或者不会成功: 1.android客户端+springMVC服务端:服务端 ...
- 考虑浏览器兼容的文件上传(IE8不支持FormData)
方法一:使用FormData(因IE8不支持FormData, IE10才支持,因此此方法不兼容IE10以下的IE浏览器) 也可参考文章 http://www.jianshu.com/p/46e6e0 ...
- WebView加载html实现网页上传本地文件(图片,拍照,语音等)
前言: 这里有两个方案,第一个使用Andorid客户端和JavaScript互相调用方法来实现,这种方法极力不推荐,它会增加服务端和客户端的开发成本. 第二种就是继承WebViewChromeClie ...
- Android(java)学习笔记214:开源框架的文件上传(只能使用Post)
1.文件上传给服务器,服务器端必然要写代码进行支持,如下: 我们新建一个FileUpload.jsp的动态网页,同时我们上传文件只能使用post方式(不可能将上传数据拼凑在url路径下),上传数据Ap ...
- 多文件上传插件Stream,是Uploadify的Flash版和Html5版的结合,带进度条,并支持html5断点续传(附件上传),拖拽等功能
是根据某网的文件上传插件加工而来,支持不同平台(Windows, Linux, Mac, Android, iOS)下,主流浏览器(IE7+, Chrome, Firefox, Safari, 其他) ...
- Android(java)学习笔记157:开源框架的文件上传(只能使用Post)
1.文件上传给服务器,服务器端必然要写代码进行支持,如下: 我们新建一个FileUpload.jsp的动态网页,同时我们上传文件只能使用post方式(不可能将上传数据拼凑在url路径下),上传数据Ap ...
随机推荐
- Domino V8 在 UNIX/Linux 平台上的安装及其常见问题
在 IBM Bluemix 云平台上开发并部署您的下一个应用. 开始您的试用 Domino V8 的安装需求 Domino V8 可以支持多种平台和操作系统,表1 列出了其支持的各种 UNIX/Lin ...
- 3.1 The Interpolating Polynomial(站点)
Cubic Interpolation: https://www.youtube.com/watch? v=EtzlEA9MIwI Quad ...
- install yael on the ubuntu 12.04
1. bits/predefs.h no such file or directory ??? sudo apt-get install gcc-multilib 2. sudo gedit /et ...
- 用R语言完成的交通可视化报告
http://sztocc.sztb.gov.cn/roadcongmore.aspx最终实现这几个图:1. 实时道路交通可视化2. 实时道路拥堵排名3. 历史路况时间序列图4. 每日每小时道况热力图 ...
- Codesys——TON和TOF的使用方法
1. 引言 介绍延迟导通.延迟关闭函数的使用方法. 2. 函数描述 TON: 当IN为FALSE时,输出Q为FALSE: 当IN为由FALSE变为TRUE时,延迟导通过程中Q为FALSE,当时间到Q变 ...
- C# winform通过按钮上移下移 解决了datasource绑定问题
事件代码: private void btn_frmDicType_MoveUp_Click(object sender, EventArgs e) { int lstLength = this.ls ...
- 2-sat总结
算法 构造一个有向图G,每个变量xi拆成两个点2i和2i+1 分别表示xi为假,xi为真 那么对于“xi为真或xj为假”这样的条件 我们就需要连接两条边 2*i —>2*j(表示如果i为假,那么 ...
- JSP-Runoob:JSP 日期处理
ylbtech-JSP-Runoob:JSP 日期处理 1.返回顶部 1. JSP 日期处理 使用JSP最重要的优势之一,就是可以使用所有Java API.本章将会详细地讲述Java中的Date类, ...
- redhat6 改 centos yum 源
**redhat的yum在线更新是收费的,如果没有注册的话不能使用,如果要使用,需将redhat的yum卸载后,重启安装其他yum源,再配置其他源.** 本文包括配置本地源及第三方源.第三方源包括:网 ...
- 使用js模拟ecshop元素挪移
<!DOCTYPE html><html><head> <title>移动</title> <script src="jqu ...