Android WebView拍照上传图片报错 showFileChooser result was already called

报错问题:
Caused by: java.lang.IllegalStateException: showFileChooser result was already called

导致原因:
1、onShowFileChooser返回值为 false 导致。解决:修改为 true
2、onReceiveValue()方法连续调用两次所致,有可能是使用相机时才去申请相关权限,申请完权限后onResume()方法会再次调用onReceiveValue()解决:增加标志位isPermissionRequest,判断是否是权限申请导致的onResume方法运行。
【2】的问题,是因为我的WebView中,拍照的弹窗是原生控件,H5 调起原生控件后,如果选取消按键,需要调用onReceiveValue()方法,传参为空,否则网页会挂住,不会再响应 H5 本身的点击事件
代码如下:

@Override
protected void onResume() {
   super.onResume();
   // 取消选择时需要回调onReceiveValue,否则网页会挂住,不会再响应点击事件
   if (mResultCode == Activity.RESULT_CANCELED && isPermissionRequest) {
     try {
         if (mUploadMessageLollipop != null) {
             mUploadMessageLollipop.onReceiveValue(null);
          }
          } catch (Exception e) {
            e.printStackTrace();
         }
   }
}

你可能感兴趣的:(Android WebView拍照上传图片报错 showFileChooser result was already called)