让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 ...
随机推荐
- Docker: Docker容器之间互相通信
最简单的方法,关闭防火墙(只建议用于开发环境) systemctl stop firewalld
- Python: 解决simple-db-migrate的"No module named 'MySQLdb'错误
sudo apt-get install libmysqlclient-dev sudo -H pip3 install mysqlclient
- PS和AI软件差别
首先.PS是处理位图图像的.AI是处理矢量图图形的.那就先说一下两者最主要的问题,位图与矢量图的差别和优缺点. 在计算机画图领域中.依据成图原理和绘制方法的不同.数字图形.图像分为"矢量图 ...
- Reduce 任务的完整数据流
- 《textanalytics》课程简单总结(1):两种word relations——Paradigmatic vs. Syntagmatic
coursera上的公开课<https://www.coursera.org/course/textanalytics>系列,讲的很不错哦. 1.两种关系:Paradigmatic vs. ...
- Zookeeper01
ZooKeeper数据模型Znode
- 01-S3C2440学习入门概念+环境搭建【转】
本文转载自:http://blog.csdn.net/fengyuwuzu0519/article/details/54754812 一.心得: 这两年学过很多东西,有点杂,总感觉不够踏实,于是准备写 ...
- [python基础] csv.wirterow()报错UnicodeEncodeError
python在安装时,默认的编码是ascii,当程序中出现非ascii编码时,python的处理常常会报这样的错,python没办法处理非ascii编码的,此时需要自己设置将python的默认编码,一 ...
- Spark SQL读parquet文件及保存
import org.apache.spark.{SparkConf, SparkContext} import org.apache.spark.sql.{Row, SparkSession} im ...
- Vue.prototype的用法
基础事例: 在vue项目main.js文件中: Vue.prototype.$appName = 'My App' 这样你可以通过在原型上定义它们使其在每个 Vue 的实例中可用. new Vue({ ...