webView加载H5,主要是解决上传图片和视频的问题。
1)Activity定义一些常量变量
private static final int REQUEST_CODE_PERMISSION_CAMERA = 0x03;
private static final int REQUEST_CODE_CAMERA = 0x02;
private static final int REQUEST_CODE_ALBUM = 0x01;
private ValueCallback<Uri[]> uploadMessageAboveL;
private ValueCallback<Uri> uploadMessage;
private String mLastPhotoPath,mCurrentPhotoPath;
private Thread mThread;
2)WebChromeClient、WebViewClient
除了实现onProgressChanged、onShowFileChooser方法外,自定义多个openFileChooser以兼容不同版本。不同版本的api调用不同的方法。
protected WebChromeClient webChromeClient = new WebChromeClient() {
@Override
public void onProgressChanged(WebView view, int newProgress) {
//关闭加载中
if (newProgress >= 100) {
dismissCustomDialog();
}
}
/**
* API >= 21(Android 5.0.1)回调此方法
*/
@Override
public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> valueCallback, FileChooserParams fileChooserParams) {
uploadMessageAboveL = valueCallback;
uploadPicture();
return true;
}
/**
* 8(Android 2.2) <= API <= 10(Android 2.3)回调此方法
*/
private void openFileChooser(ValueCallback<Uri> uploadMsg) {
uploadMessage = uploadMsg;
uploadPicture();
}
/**
* 11(Android 3.0) <= API <= 15(Android 4.0.3)回调此方法
*/
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
openFileChooser(uploadMsg);
}
/**
* 16(Android 4.1.2) <= API <= 20(Android 4.4W.2)回调此方法
*/
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
openFileChooser(uploadMsg);
}
};
protected WebViewClient webViewClient = new WebViewClient() {
@Override
public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
//让一般的http和https协议开头的走正常的页面,而其他的URL则会开启一个Acitity然后去调用原生APP应用
if (url.startsWith("http") || url.startsWith("https")) {
return super.shouldInterceptRequest(view, url);
} else {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return null;
}
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// 表明点击网页里面的链接不调用系统浏览器,而是在本WebView中显示
view.loadUrl(url);//加载需要显示的网页
return true;
}
@Override
public void onPageFinished(WebView view, String url) {
//页面完成加载
super