因为android的webview无法像ios一样,自动识别camera字段,从而弹出对应的照相选项框,这就要我们自己实现了。
实现方式网络上很详尽,这里主要记录一个坑,就是在自定义的弹框选择取消,或者拍照撤销,回到webview界面,再次点击发现,自定义的底部弹框不会再次弹出了,这是怎回事呢?主要是因为valueCallback没有在取消的情况下没有将它的onReceiveValue赋为null,所以他就不会再次调用它的onActivityResult以及onActivityResultAboveL方法,也就导致了再次点击无反应。
最后贴一下代码:
//低版本选取回来的是Uri
private ValueCallback uploadFile;
//高版本选取回来的是Uri数组
private ValueCallback uploadFileAboveL;
/**
*识别camera字段方法
*/
web_insurance.setWebChromeClient(new
WebChromeClient() {
//For Android >= 4.1
@Override
public void openFileChooser (ValueCallback valueCallback, String acceptType, String capture)
{
LogUtils.e("openFileChooser", "For Android >= 4.1");
uploadFile = valueCallback;
openImageChooserActivity();
}
// For Android >= 5.0
@Override
public boolean onShowFileChooser (WebView webView, ValueCallback valueCallback, FileChooserParams fileChooserParams){
LogUtils.e("openFileChooser", "For Android >= 5.0");
uploadFileAboveL = valueCallback;
openImageChooserActivity();
return true;
}
});
/**
* 打开选取的方法
*/
private void openImageChooserActivity() {
showPopWin();
}
private void showPopWin() {
// 利用layoutInflater获得View
if (window == null) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
@SuppressLint("InflateParams") final
View popview = inflater.inflate(R.layout.pop_window_choose_headportrait, null);
// 得到宽度和高度
window = new PopupWindow(popview,
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT);
// 设置popWindow弹出窗体可点击,这句话必须添加,并且是true
window.setFocusable(true);
window.setOutsideTouchable(false);
// 实例化一个ColorDrawable颜色为半透明
// noinspection deprecationP
window.setBackgroundDrawable(new BitmapDrawable());
backgroundAlpha(0.8f);
// 设置popWindow的显示和消失动画
window.setAnimationStyle(R.style.mypopwindow_anim_style);
window.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
backgroundAlpha(1f);
}
});
TextView tvPhoto = (TextView) popview.findViewById(R.id.tv_photograph);
tvAlbum = (TextView) popview.findViewById(R.id.tv_select_from_album);
//
tvSave = (TextView) popview.findViewById(R.id.tv_save_photo);
tvSave.setVisibility(View.GONE);
//
cancel = (Button) popview.findViewById(R.id.cancel);
// 调用相机的按钮监听
tvPhoto.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
callCamera();
}
});
// 调用相册的按钮监听
tvAlbum.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//noinspection deprecation
tvAlbum.setTextColor(getResources().getColor(R.color.yunda_text_new));
callAlbum();
}
});
// 取消
cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (CommonUtil.notNull(window) && window.isShowing()) {
//noinspection deprecation
// cancel.setTextColor(getResources().getColor(R.color.yunda_text_new));
window.dismiss();
//取消的时候回调参数赋为null
if (uploadFileAboveL != null) {
uploadFileAboveL.onReceiveValue(null);
uploadFileAboveL = null;
} else if (uploadFile != null) {
uploadFile.onReceiveValue(null);
uploadFile = null;
}
}
}
});
}
// 在底部显示
window.showAtLocation(this.findViewById(R.id.common_webview),
Gravity.BOTTOM, 0, 0);
}
/**
* 相机选取回执
*/
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == FILE_CHOOSER_RESULT_CODE) {
if (null == uploadFile && null == uploadFileAboveL) return;
Uri result = data == null || resultCode != Activity.RESULT_OK ? null : data.getData();
if (uploadFileAboveL != null) {
onActivityResultAboveL(requestCode, resultCode, data);
} else if (uploadFile != null) {
uploadFile.onReceiveValue(result);
uploadFile = null;
}
}
}
//这里intent.getClipData()方法需要在api16以上才能使用这个
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
private void onActivityResultAboveL(int requestCode, int resultCode, Intent intent) {
if (requestCode != FILE_CHOOSER_RESULT_CODE || uploadFileAboveL == null)
return;
Uri[] results = null;
if (resultCode == Activity.RESULT_OK) {
if (intent != null) {
String dataString = intent.getDataString();
ClipData clipData = intent.getClipData();
if (clipData != null) {
results = new Uri[clipData.getItemCount()];
for (int i = 0; i < clipData.getItemCount(); i++) {
ClipData.Item item = clipData.getItemAt(i);
results[i] = item.getUri();
}
}
if (dataString != null)
results = new Uri[]{Uri.parse(dataString)};
}
}
uploadFileAboveL.onReceiveValue(results);
uploadFileAboveL = null;
}