混合开发 webview 中file 控件 点击后无反应解决方法

时间:2023-03-09 00:48:48
混合开发 webview 中file 控件 点击后无反应解决方法

最近在做个项目 ,需要 使用 file 控件上传 图片到服务器 ,在手机浏览器中 可以正常选择照片,但是放到 android 应用中的webview中,file 控件点击后就没有反应。

百度了一番后,找到以下解决方案

开头定义

private ValueCallback<Uri> mUploadMessage;
final static int FILE_SELECTED = 4;

然后设置

mWebView.setWebChromeClient(new WebChromeClient() {
public void openFileChooser(ValueCallback<Uri> uploadMsg,
String acceptType, String capture) {
mUploadMessage = uploadMsg;
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("image/*");
MainActivity.this.startActivityForResult(
Intent.createChooser(intent, "完成操作需要使用"),
FILE_SELECTED);
}
});
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent intent) { switch (requestCode) { // Choose a file from the file picker.
case FILE_SELECTED:
if (null == mUploadMessage)
break;
Uri result = intent == null || resultCode != RESULT_OK ? null
: intent.getData();
mUploadMessage.onReceiveValue(result);
mUploadMessage = null;
break;
}
}

在activty中加入 以上代码, file 控件可以点开了,可以选择 图片库 和 文件库中的文件 ,但是无法调用相机。

目前只能解决到这一步 ,如果有人知道怎么调用相机,请告诉我,谢谢!

http://blog.csdn.net/woshinia/article/details/19030437  这篇博文貌似说解决了,但是我一直没调试通