Android WebView 不支持 H5 input type="file" 解决方法
来源:互联网 发布:c语言float的取值范围 编辑:程序博客网 时间:2024/06/10 15:12
最近因为赶项目进度,因此将本来要用原生控件实现的界面,自己做了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); } });}
@Overridepublic 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();}
6 0
- Android WebView 不支持 H5 input type="file" 解决方法
- Android WebView 不支持 H5 input type="file" 解决方法
- Android WebView 不支持 H5 input type="file" 解决方法
- Android WebView不支持H5输入type =“file”解决方法
- Android WebView H5页面 input type =“file”解决方法
- Android WebView 支持H5图片上传<input type="file">
- 解决WebView不支持input type=file
- Android webview Input type=file 文件上传 解决方法
- 解决安卓webview不支持input type=file问题
- 解决安卓webview不支持input type=file问题
- android Webview支持input type=file
- android Webview支持input type=file
- android webview支持<input type="file"类型
- Android WebView支持Input type=file文件上传
- Android:让WebView支持<input type=”file”…>元素
- Android:让WebView支持<input type=”file”…>元素
- 使用Android的webview将web app打包成安卓的app和解决安卓webview不支持input type=file问题
- android webview使用html5<input id="input" type="file"/> 上传相册、拍照照片
- 怎样写一个增广矩阵到txt文件中
- wowza 配置自己的VOD-Edge 实现一个vod点播
- CSS
- 基于springjdbc封装的javaee轻量级开发平台jmapper(一)
- C++作业5
- Android WebView 不支持 H5 input type="file" 解决方法
- ubuntu下的hadoop安装
- session 和 cookie
- android Layout技巧汇总
- 修改tomcat默认页面
- Leetcode 9. Palindrome Number
- 人数不定的工资类
- 函数sscanf()及sprintf()的简单讲解 --- NOJ 2015 PUMA
- 2dx Spine清除动画残影