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;
}
}