1、加载网页后显示白板问题
//设置允许DOM存储
webSettings.setDomStorageEnabled(true);
2、选择文件无响应
2.1 重写WebChromeClient中openFileChooser和onShowFileChooser方法;
mWebView.setWebChromeClient(new WebChromeClient() {
// 3.0 + 调用这个方法
public void openFileChooser(ValueCallback filePathCallback, String acceptType) {
getFile(filePathCallback);
}
// Android > 4.1.1 调用这个方法
public void openFileChooser(ValueCallback<Uri> filePathCallback, String acceptType, String capture) {
getFile(filePathCallback);
}
// Android > 5.0 调用这个方法
@Override
public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) {
getFile(filePathCallback);
return true;
}
});
2.2 跳转到选择文件的页面
//声明文件路径接收回调变量
private ValueCallback valueCallback;
//跳转到选择文件的页面
public void getFile(ValueCallback valueCallback) {
//接收文件路径的回调
this.valueCallback = valueCallback;
Intent intent;
//可以自己定义要跳转的文件系统页面
if( Build.VERSION.SDK_INT >= 29 && this.getApplicationInfo().targetSdkVersion >= 29) {
intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
}else {
intent = new Intent(Intent.ACTION_GET_CONTENT);
}
intent.setType("image/*");
intent.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(intent, REQUEST_CODE);
}
2.3 重写onActivityResult方法
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE){
if (resultCode == Activity.RESULT_OK) {
selectH5File(data, valueCallback);
}
}
}
2.4 接收选择的文件路径
public void selectH5File(Intent data, ValueCallback valueCallback){
if (valueCallback == null){
return;
}
try {
Uri[] results = null;
String dataString = data.getDataString();
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
ClipData clipData = data.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)};
valueCallback.onReceiveValue(results);
}
}catch (Exception e){
e.printStackTrace();
}
valueCallback = null;
}
3、文件取消选择后再次点击无响应
重写onActivityResult方法:
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE){
if (resultCode == Activity.RESULT_OK) {
selectH5File(data, valueCallback);
} else {
//取消选择
if (valueCallback != null){
valueCallback.onReceiveValue(null);
valueCallback = null;
}
}
}
}
网友评论