android下webview实现H5 html标签 input file类型文件单选多选  multiple 属性的支持

资源有限 仅在华为手机上进行测试

private final static int  REQUEST_SELECT_FILE = 2;private final static int FILECHOOSER_RESULTCODE = 1;private ValueCallback uploadMessage; //多选文件回调private ValueCallback mUploadMessage ; //单选文件回调webView.setWebChromeClient(new WebChromeClient() {// For 3.0+ Devices (Start)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 目测应该5.0+才支持多选@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)public boolean onShowFileChooser(WebView mWebView, ValueCallback filePathCallback, WebChromeClient.FileChooserParams fileChooserParams){if (uploadMessage != null) {uploadMessage.onReceiveValue(null);uploadMessage = null;}uploadMessage = filePathCallback;Intent intent = fileChooserParams.createIntent();//此处增加参数  允许多选文件intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE,true);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 onlyprotected void openFileChooser(ValueCallback 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);}//for Android <3.0protected void openFileChooser(ValueCallback 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);}});@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent intent) {super.onActivityResult(requestCode, resultCode, intent);if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){if (requestCode == REQUEST_SELECT_FILE){if (uploadMessage == null || intent == null) {                //若没有选择文件就回退  执行清空  不进行这步操作无法再次选择文件                uploadMessage.onReceiveValue(null);                uploadMessage = null;                return;            }//由于用户可能单选或多选   单选getData  多选时getClipData()if(intent.getData() != null){uploadMessage.onReceiveValue(WebChromeClient.FileChooserParams.parseResult(resultCode, intent));}else if(intent.getClipData() != null){Uri[] uris = new Uri[intent.getClipData().getItemCount()];for(int i=0;i

完善了    选择文件时    未选择文件   操作回退按钮  的支持

2019年1月4日

完善了  用户操作未选择文件   回退后无法再次选择文件的问题  主要是在onActivityResult内进行onReceiveValue(null)

更多相关文章

  1. 一款常用的 Squid 日志分析工具
  2. GitHub 标星 8K+!一款开源替代 ls 的工具你值得拥有!
  3. RHEL 6 下 DHCP+TFTP+FTP+PXE+Kickstart 实现无人值守安装
  4. Linux 环境下实战 Rsync 备份工具及配置 rsync+inotify 实时同步
  5. Android(安卓)logcat命令使用
  6. android 根据apk文件获取包名等信息 已经判断 指定 这个apk 是否
  7. sqlite3 常用命令 for android
  8. Android(安卓)字体自适应设置
  9. Android(安卓)R文件

随机推荐

  1. Android测试驱动开发实践1
  2. android项目中每个文件的作用
  3. 英特尔® Android* USB 驱动程序安装说明
  4. 也谈Android的学习和利用Android来赚钱
  5. android平板上的GridView视图缓存优化
  6. 【Android】Android(安卓)4.0 无法接收开
  7. Android各种资源详解
  8. 详解 Android(安卓)的 Activity 组件
  9. Android(安卓)自己动手写ListView学习其
  10. Android中文合集(5)(126+8篇)(chm格式)