因為android的webview無法像ios一樣弃甥,自動識別camera字段爽室,從而彈出對應(yīng)的照相選項框,這就要我們自己實現(xiàn)了淆攻。
實現(xiàn)方式網(wǎng)絡(luò)上很詳盡阔墩,這里主要記錄一個坑,就是在自定義的彈框選擇取消卜录,或者拍照撤銷戈擒,回到webview界面,再次點擊發(fā)現(xiàn)艰毒,自定義的底部彈框不會再次彈出了筐高,這是怎回事呢?主要是因為valueCallback沒有在取消的情況下沒有將它的onReceiveValue賦為null丑瞧,所以他就不會再次調(diào)用它的onActivityResult以及onActivityResultAboveL方法柑土,也就導(dǎo)致了再次點擊無反應(yīng)。
最后貼一下代碼:
//低版本選取回來的是Uri
private ValueCallback uploadFile;
//高版本選取回來的是Uri數(shù)組
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);
// 設(shè)置popWindow彈出窗體可點擊绊汹,這句話必須添加稽屏,并且是true
window.setFocusable(true);
window.setOutsideTouchable(false);
// 實例化一個ColorDrawable顏色為半透明
// noinspection deprecationP
window.setBackgroundDrawable(new BitmapDrawable());
backgroundAlpha(0.8f);
// 設(shè)置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);
// 調(diào)用相機的按鈕監(jiān)聽
tvPhoto.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
callCamera();
}
});
// 調(diào)用相冊的按鈕監(jiān)聽
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();
//取消的時候回調(diào)參數(shù)賦為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);
}
/**
* 相機選取回執(zhí)
*/
@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;
}