让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 ...
随机推荐
- Ubuntu下用hadoop2.4搭建集群(伪分布式)
要真正的学习hadoop,就必需要使用集群,可是对于普通开发人员来说,没有大规模的集群用来測试,所以仅仅能使用伪分布式了.以下介绍怎样搭建一个伪分布式集群. 为了节省时间和篇幅,前面一些步骤不再叙述. ...
- 网页图表Highcharts实践教程之标签组与加载动画
网页图表Highcharts实践教程之标签组与加载动画 Highcharts标签组 在图表的大部分元素都提供了标签功能.但非常多时候,我们须要额外说明一些信息.这个时候借助原有的图表元素的标签功能就 ...
- click事件触发也有失灵的时候?
今天做了个手机页面,点击某个按钮->弹出菜单,再点击菜单以外的任意位置->关闭菜单,在其他浏览器里面没有问题,但是在IOS浏览器中并不会关闭. 网上解决这个bug的帖子很多,这篇帖子主要是 ...
- Codeforces Round #211 (Div. 2)B. Fence
B. Fence time limit per test 1 second memory limit per test 256 megabytes input standard input outpu ...
- kentico9开始移除的webpart
https://devnet.kentico.com/articles/fighting-featuritis https://blog.intercom.com/product-strategy-m ...
- eclipse高亮选中属性以及更改颜色
1.显示: 1.1.工具栏里有个黄色小笔的图标,点一下就好了 1.2.打开对话框windows->preference,在左上角输入mark Occurrencs 把右边都选 ...
- Anaconda 安装和使用Numpy、Scipy、pandas、Scikit-learn
Anaconda 安装和使用 https://www.cnblogs.com/liruihuan/p/9101613.html 最近看了些关于数据分析的书,想系统的整理下相关知识,算是学习笔记吧,也希 ...
- PKUACM2018 A Wife——DP
题目:http://poj.openjudge.cn/practice/C18A/ 据说正解是差分约束,转化的过程还要用到标准型.对偶型什么的知识,暂时还不太懂... 但也有贪心DP做法,有个结论:一 ...
- SmartDispatcher 类
UI线程中使用 public class SmartDispatcher { public static void BeginInvoke(Action action) { if (Deploymen ...
- 71.Ext.form.ComboBox 完整属性
转自:https://blog.csdn.net/taotaoqi/article/details/7409514 Ext.form.ComboBox 类全称: Ext.form.ComboBox 继 ...