webView文件上传取消弹窗后再次点击不响应点击事件问题
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了webView文件上传取消弹窗后再次点击不响应点击事件问题相关的知识,希望对你有一定的参考价值。
参考技术A 移动端H5页面调用手机相册或者照相机后,再次点击没反应问题,这是需要安卓方面修改的,前端不需要修改。实现方法是
webView_customer.setWebChromeClient(new WebChromeClient()
// For android 3.0+
public void openFileChooser(ValueCallback uploadMsg, String acceptType)
mUploadMessage = uploadMsg;
ArrayList list = new ArrayList();
list.add("拍照");
list.add("从相册选择");
showTakePicOrPhotoesDialog(Activity_Customer_Edu.this, "选择", list, new DialogUtils.OnClickPicDialogItemListener()
@Override
public void onClickItem(int which)
if(0==which)
Intent takeIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//下面这句指定调用相机拍照后的照片存储的路径
try
File file = createImageFile();
takePicUri = Uri.fromFile(file);
takeIntent.putExtra(MediaStore.EXTRA_OUTPUT, takePicUri);
startActivityForResult(takeIntent, FILECHOOSER_RESULTCODE);
catch (IOException e)
e.printStackTrace();
else if(1==which)
Intent pickIntent = new Intent(Intent.ACTION_PICK, null); pickIntent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
startActivityForResult(pickIntent, FILECHOOSER_RESULTCODE);
);
// For Android < 3.0
public void openFileChooser(ValueCallback uploadMsg)
openFileChooser(uploadMsg, "");
// For Android > 4.1.1
public void openFileChooser(ValueCallback uploadMsg, String acceptType, String capture)
openFileChooser(uploadMsg, acceptType);
// For Android 5.0+
public boolean onShowFileChooser (WebView webView, ValueCallback filePathCallback, WebChromeClient.FileChooserParams fileChooserParams)
mUploadCallbackAboveL = filePathCallback;
ArrayList list = new ArrayList();
list.add("拍照");
list.add("从相册选择");
showTakePicOrPhotoesDialog(Activity_Customer_Edu.this, "选择", list, new DialogUtils.OnClickPicDialogItemListener()
@Override
public void onClickItem(int which)
if(0==which)
Intent takeIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//下面这句指定调用相机拍照后的照片存储的路径
try
File file = createImageFile();
takePicUri = Uri.fromFile(file);
takeIntent.putExtra(MediaStore.EXTRA_OUTPUT, takePicUri);
startActivityForResult(takeIntent, FILECHOOSER_RESULTCODE);
catch (IOException e)
e.printStackTrace();
else if(1==which)
Intent pickIntent = new Intent(Intent.ACTION_PICK, null);
pickIntent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
startActivityForResult(pickIntent, FILECHOOSER_RESULTCODE);
);
return true;
);
OnActivityResult回调方法:这里有个特别注意的地方,当打开拍照按钮时,Intent data这个数据是空,所以不能正常上传,打开拍照的Intent当时传了一个Uri,在OnActivityResult需要判断data是不是空,是空的话,就把传的那个Uri赋值给mUploadMessage.onReceiveValue,如果不为空就传data.getData();具体如下:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
if(requestCode==FILECHOOSER_RESULTCODE)
Uri result;
if (null == mUploadMessage && null == mUploadCallbackAboveL)
return;
//如果拍完照点击取消按钮就:if(resultCode ==RESULT_CANCELED) result = Uri.EMPTY;elseif(data ==null&& resultCode !=RESULT_OK) result = Uri.EMPTY; else if(data ==null) result =takePicUri; else result = data.getData();
if (mUploadMessage != null)
mUploadMessage.onReceiveValue(result);
mUploadMessage = null;
if(mUploadCallbackAboveL!=null) mUploadCallbackAboveL.onReceiveValue(newUri[]result);mUploadCallbackAboveL=null;
这里有一个问题,点击“取消”或返回按钮,无法重复回调onShowFileChooser或openFileChooser方法。解决办法:
定义一个方法:
/**
*防止点击dialog的取消按钮之后,就不再次响应点击事件了
*/
public static void cancelCallback()
if(mUploadCallbackAboveL!=null)
mUploadCallbackAboveL.onReceiveValue(null);
if(mUploadMessage!=null)
mUploadMessage.onReceiveValue(null);
在弹窗(dialog)的取消按钮监听的方法中调用上面的cancellCallBack方法。比如我的:
/**
*展示拍照或者从相册选择的Dialog
* @param context
* @param title
* @param names
* @param onClickPicDialogItemListener
*/
public static void showTakePicOrPhotoesDialog(Context context, String title, final ArrayList names, final DialogUtils.OnClickPicDialogItemListener onClickPicDialogItemListener)
AlertDialog.Builder builder=new AlertDialog.Builder(context);
builder.setTitle(title);//设置标题
String[] itemList=(String[]) names.toArray(new String[names.size()]);
builder.setItems(itemList, new DialogInterface.OnClickListener()
@Override
public void onClick(DialogInterface dialog, int which)
onClickPicDialogItemListener.onClickItem(which);
);
AlertDialog dialog=builder.create();//获取dialog
dialog.setOnCancelListener(new DialogInterface.OnCancelListener()
@Override
public void onCancel(DialogInterface dialog)
cancelCallback();
);
dialog.show();//显示对话框
以上是关于webView文件上传取消弹窗后再次点击不响应点击事件问题的主要内容,如果未能解决你的问题,请参考以下文章