Android WebView 不支持 H5 input type="file" 解决方法

最近因为赶项目进度,因此将本来要用原生控件实现的界面,自己做了H5并嵌入webview中。发现点击H5中 input type="file" 标签 不能打开android资源管理器。

通过网络搜索发现是因为 android webview 由于考虑安全原因屏蔽了 input type="file" 这个功能 。

经过不懈的努力,以及google 翻译的帮助 在 stackoverflow 中找到了解决的方法。

具体可以理解为 重写webview 的WebChromeClient ,废话不多说直接贴代码:

private ValueCallback<Uri> mUploadMessage; public ValueCallback<Uri[]> uploadMessage; public static final int REQUEST_SELECT_FILE = 100; private final static int FILECHOOSER_RESULTCODE = 2; webview.setWebChromeClient(new WebChromeClient(){ // For 3.0+ Devices (Start) // onActivityResult attached before constructor protected void openFileChooser(ValueCallback uploadMsg, String acceptType) { mUploadMessage = uploadMsg; Intent i = new Intent(Intent.ACTION_GET_CONTENT); i.addCategory(Intent.CATEGORY_OPENABLE); i.setType("image/*"); startActivityForResult(Intent.createChooser(i, "File Browser"), FILECHOOSER_RESULTCODE); } // For Lollipop 5.0+ Devices @TargetApi(Build.VERSION_CODES.LOLLIPOP) public boolean onShowFileChooser(WebView mWebView, ValueCallback<Uri[]> filePathCallback, WebChromeClient.FileChooserParams fileChooserParams) { if (uploadMessage != null) { uploadMessage.onReceiveValue(null); uploadMessage = null; } uploadMessage = filePathCallback; Intent intent = fileChooserParams.createIntent(); try { startActivityForResult(intent, REQUEST_SELECT_FILE); } catch (ActivityNotFoundException e) { uploadMessage = null; Toast.makeText(getBaseContext(), "Cannot Open File Chooser", Toast.LENGTH_LONG).show(); return false; } return true; } //For Android 4.1 only protected 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/*"); startActivityForResult(Intent.createChooser(intent, "File Browser"), FILECHOOSER_RESULTCODE); } protected void openFileChooser(ValueCallback<Uri> uploadMsg) { mUploadMessage = uploadMsg; Intent i = new Intent(Intent.ACTION_GET_CONTENT); i.addCategory(Intent.CATEGORY_OPENABLE); i.setType("image/*"); startActivityForResult(Intent.createChooser(i, "File Chooser"), FILECHOOSER_RESULTCODE); } }); } @Override public void onActivityResult(int requestCode, int resultCode, Intent intent) { if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { if (requestCode == REQUEST_SELECT_FILE) { if (uploadMessage == null) return; uploadMessage.onReceiveValue(WebChromeClient.FileChooserParams.parseResult(resultCode, intent)); uploadMessage = null; } } else if (requestCode == FILECHOOSER_RESULTCODE) { if (null == mUploadMessage) return; // Use MainActivity.RESULT_OK if you're implementing WebView inside Fragment // Use RESULT_OK only if you're implementing WebView inside an Activity Uri result = intent == null || resultCode != MainActivity.RESULT_OK ? null : intent.getData(); mUploadMessage.onReceiveValue(result); mUploadMessage = null; } else Toast.makeText(getBaseContext(), "Failed to Upload Image", Toast.LENGTH_LONG).show(); }

以上所述是小编给大家介绍的Android WebView 不支持 H5 input type="file" 解决方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

时间: 2024-07-30 19:00:37

Android WebView 不支持 H5 input type="file" 解决方法的相关文章

前端常见兼容问题系列6: 一些安卓APP的WebView中&lt;input type=&quot;file&quot;&gt;不工作

有一次想做一个HTML5的图片上传功能,主要是依赖<input type="file">来选择本地的图片.开始一切都很顺利,在浏览器中,在淘宝等容器中,都能如预期进行文件选择和上传.在iOS系统下的APP中也都工作正常,但是在一个安卓APP中,无论怎么尝试选择文件,都始终出不来. 我给input的onchange事件打了个alert(),点击时也没有任何反应.看来是<input type="file">在这个容器中根本就不工作. 经过一番检索

html js java-如何获取Input type=file 所选择文件的绝对路径

问题描述 如何获取Input type=file 所选择文件的绝对路径 是这样的 现在做的一个项目,页面很简单 左边一个input输入框 可以在里面输入url 右边一个search按钮 当输入框没有输入东西的时候,点击按钮弹出一个上传文件选择的框 所选择的文件的 绝对路径会显示在输入框里 如果输入框有东西 URL是否存在,如果存在 则弹出的上传文件选择框定位在所输入的URL上面 但是现在第一步我就不知道怎么做了,除了ie 别的浏览器选择的文件都只显示文件名 请问有什么办法可以获取到所选择文件的绝

兼容各浏览器的 input type=file 文件域美化

样式:  代码如下 复制代码 .fileInput{width:102px;height:34px; background:url(http://images.cnblogs.com/cnblogs_com/dreamback/upFileBtn.png);overflow:hidden;position:relative;} .upfile{position:absolute;top:-100px;} .upFileBtn{width:102px;height:34px;opacity:0;f

firefox下input type=&quot;file&quot;的size是多大[原]

firefox对type="file" 的input的width定义目前是不支持的,但是FF支持size属性,可以给size设置一个值,来控制上传框的大小. 但是这个size值怎么设置,size="10"是多宽,默认值又是多少,不能光凭感觉去设置. 用脚本来查看下:       <script type="text/javascript">         $(function() {             var fileArra

mvc-asp.net MVC怎么给图input type=file传过来的图片加水印

问题描述 asp.net MVC怎么给图input type=file传过来的图片加水印 解决方案 加水印后,在controller里面传到服务器里面保存 解决方案二: 这是controller的代码 public ActionResult AddNewsPic(string id) { if (Request.Files.Count > 0) { if (!string.IsNullOrEmpty(Request.Files[0].FileName)) { Stream fileDataStr

js 获取、清空input type=&quot;file&quot;的值

 本篇文章主要介绍了js 获取.清空input type="file"的值(示例代码) 需要的朋友可以过来参考下,希望对大家有所帮助 上传控件(<input type="file"/>)用于在客户端浏览并上传文件,用户选取的路径可以由value属性获取,但value属性是只读的,不能通过 javascript来赋值,这就使得不能通过value=""语句来清空它.很容易理解为什么只读,如果可以随意赋值的话,那么用户只要打开你的网页, 你

js 获取、清空input type=&quot;file&quot;的值示例代码

 本篇文章主要是对js获取.清空input type="file"的值的示例代码进行了介绍,需要的朋友可以过来参考下,希望对大家有所帮助 上传控件基础知识说明:   上传控件(<input type="file"/>)用于在客户端浏览并上传文件,用户选取的路径可以由value属性获取,但value属性是只读的,不能通过javascript来赋值,这就使得不能通过value=""语句来清空它.很容易理解为什么只读,如果可以随意赋值的话,

jQuery动态添加 input type=file的实现代码_jquery

复制代码 代码如下: <form id="fileForm" action="" method="post" enctype="multipart/form-data"> <tr> <td> <input type="file" name="file"><input type="button" id="a

判断多个input type=file是否有已经选择好文件的代码_jquery

表单中有多个<input type="file" name="uploadfile" contentEditable="false" style="width:80%">, 提交表单时需要判断其中至少要有一个input已经选择好文件. 复制代码 代码如下: <input type="file" name="uploadfile" contentEditable=&quo