android5.0webview上传图片的几个小问题

public class MCustomChromeClient extends WebChromeClient {


      // 3.0+
      public void openFileChooser(ValueCallback<Uri> uploadMsg,String acceptType) {
              mUploadMsg = uploadMsg;
              mAcceptType = VERSION_TAG;
               showDialog();
      }

 

        //3.0-

         public void openFileChooser(ValueCallback<Uri> uploadMsg) {
                openFileChooser(uploadMsg, "");
         }


       // 4.0+
       public void openFileChooser(ValueCallback<Uri> uploadMsg,String acceptType, String capture) {
              openFileChooser(uploadMsg, acceptType);
       }

     /**
      * 5.0+
      */
       public boolean onShowFileChooser(WebView webView,ValueCallback<Uri[]> filePathCallback,
                 FileChooserParams fileChooserParams) {
                       mUploadMsgFive = filePathCallback;
                       mFileChooserParams = fileChooserParams;
                       showDialog();
                       return true;
       }

}

 

注意:5.0以上的版本重写onShowFileChooser方法,注意一定要返回true;

showDialog(); 这个是弹出对话框 选择献策选取还是拍照

通过拍照的方式,会产生一个问题, 在横竖屏切换时会销毁当前界面,重新创建,致使数据失效(回调接口为空)

通过在XML当中加上  android:configChanges="keyboardHidden|screenSize|orientation".

这样解决了界面数据问题,但是返回结果为空通过如下代码解决

private void getPicByCamera() {
         Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
         imagePaths = Environment.getExternalStorageDirectory().getPath() + "路径" + (System.currentTimeMillis() + ".jpg");

         File newFile = new File(imagePaths);
        if (!newFile.exists()) {
                   File vDirPath = newFile.getParentFile();
                   vDirPath.mkdirs();
        } else {
                  if (newFile.exists()) {
                             newFile.delete();
                    }
       }
      cameraUri = Uri.fromFile(newFile);
      intent.putExtra(MediaStore.EXTRA_OUTPUT, cameraUri);
      startActivityForResult(Intent.createChooser(intent, "File Chooser"),REQUEST_CODE_CAMERA);
}

将图片的存储路径提前建好,拍照结束后直接用cameraUri,而不再通过onActivityResult当中的 data来取.

protected void onActivityResult(int requestCode, int resultCode,data) {
           super.onActivityResult(requestCode, resultCode, data);
           Uri uri = data == null || resultCode != RESULT_OK ? null: data.getData();
           switch (requestCode) {
                   case REQUEST_CODE_CHOICE:
                   if (resultCode == RESULT_OK) {
                          if(mAcceptType!=null&&mAcceptType.equals(VERSION_TAG)) {
                                  mUploadMsg.onReceiveValue(uri);
                                  mUploadMsg = null;
                           }else{
                                   mUploadMsgFive.onReceiveValue(
                                   mFileChooserParams.parseResult(resultCode, data));
                                   mUploadMsgFive = null;
                          }
                     } else {
                           reSet();
           }
                  break;


          case REQUEST_CODE_CAMERA:
                   if (resultCode == RESULT_OK) {
                          if(mAcceptType!=null&&mAcceptType.equals(VERSION_TAG)) {
                                   mUploadMsg.onReceiveValue(cameraUri);
                                   mUploadMsg = null;
                           }else{
                                   Uri[] uris = new Uri[] { cameraUri };
                                   mUploadMsgFive.onReceiveValue(uris);
                                    mUploadMsgFive = null;
                            }
                    } else {
                              reSet();
                  }
                   break;


           default:
                   break;
          }
}

 

reSet方法为解决在选取文件过程中中途返回, 而界面一直在等结果,导致界面上的按钮不可点击.

private void reSet() {
          if (mUploadMsg != null) {
                  mUploadMsg.onReceiveValue(null);
                  mUploadMsg = null;
           }


          if (mUploadMsgFive != null) {
                   mUploadMsgFive.onReceiveValue(null);
                   mUploadMsgFive = null;
            }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值