今天公司突然上传了一个网页有上传图片功能,但是WebView没有支持,没办法只能再写些代码。然后就ok了,效果如下图所示:
实现关键代码如下所示:
@SuppressLint("NewApi")
private void initmWebView() {
mWebView.getSettings().setJavaScriptEnabled(true); // enable javascript
//支持H5本地存储 enable local storage
mWebView.getSettings().setDatabaseEnabled(true);
String path = getApplicationContext().getDir("cache", Context.MODE_PRIVATE).getPath();
mWebView.getSettings().setDatabasePath(path);
mWebView.getSettings().setDomStorageEnabled(true);
mWebView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
mWebView.getSettings().setUseWideViewPort(true);
mWebView.getSettings().setLoadWithOverviewMode(true);
mWebView.getSettings().setSupportZoom(true);
mWebView.getSettings().setBuiltInZoomControls(true);
if (android.os.Build.VERSION.SDK_INT >= 11) {
mWebView.getSettings().setDisplayZoomControls(false);
} else {
// 3.0一下可用反射解决
}
mWebView.getSettings().setAppCacheEnabled(true);
mWebView.requestFocus();
mWebView.setWebChromeClient(wcc);
mWebView.setDownloadListener(dl);
mWebView.setWebViewClient(wvc);
mWebView.loadUrl(url);
}
private ValueCallback<Uri> mUploadMessage;
private final static int FILECHOOSER_RESULTCODE = 1;
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == FILECHOOSER_RESULTCODE) {
if (null == mUploadMessage)
return;
Uri result = intent == null || resultCode != RESULT_OK ? null : intent.getData();
mUploadMessage.onReceiveValue(result);
mUploadMessage = null;
}
}
private WebChromeClient wcc = new WebChromeClient() {
@SuppressWarnings("unused")
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/*");
startActivityForResult(Intent.createChooser(intent, "完成操作需要使用"), FILECHOOSER_RESULTCODE);
}
@SuppressWarnings("unused")
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
mUploadMessage = uploadMsg;
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("image/*");
startActivityForResult(Intent.createChooser(intent, "完成操作需要使用"), FILECHOOSER_RESULTCODE);
}
@SuppressWarnings("unused")
public void openFileChooser(ValueCallback<Uri> uploadMsg) {
mUploadMessage = uploadMsg;
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("image/*");
startActivityForResult(Intent.createChooser(intent, "完成操作需要使用"), FILECHOOSER_RESULTCODE);
}
//网页进度情况
public void onProgressChanged(WebView view, int newProgress) {
if (newProgress < 100) {
mBar.setVisibility(View.VISIBLE);
mBar.setProgress(newProgress);
} else {
mBar.setVisibility(View.GONE);
}
};
public boolean onJsAlert(WebView view, String url, String message, final android.webkit.JsResult result) {
AlertDialog.Builder b2 = new AlertDialog.Builder(MainActivity.this).setTitle("温馨提示").setMessage(message).setPositiveButton("确认", new AlertDialog.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
result.confirm();
}
});
b2.setCancelable(false);
b2.create();
b2.show();
return true;
};
public boolean onJsPrompt(WebView view, String url, final String message, String defaultValue, JsPromptResult result) {
// webView.stopLoading();
// webView.reload();
// result.confirm(); //如果自己处理js 则最后必须result.confirm
return false;
};
};
openFileChooser方法写这么多个就是为了适配不同的版本,需要的朋友们,可以看看。