1、加載網(wǎng)頁(yè)后顯示白板問(wèn)題
//設(shè)置允許DOM存儲(chǔ)
webSettings.setDomStorageEnabled(true);
2践盼、選擇文件無(wú)響應(yīng)
2.1 重寫WebChromeClient中openFileChooser和onShowFileChooser方法;
mWebView.setWebChromeClient(new WebChromeClient() {
// 3.0 + 調(diào)用這個(gè)方法
public void openFileChooser(ValueCallback filePathCallback, String acceptType) {
getFile(filePathCallback);
}
// Android > 4.1.1 調(diào)用這個(gè)方法
public void openFileChooser(ValueCallback<Uri> filePathCallback, String acceptType, String capture) {
getFile(filePathCallback);
}
// Android > 5.0 調(diào)用這個(gè)方法
@Override
public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) {
getFile(filePathCallback);
return true;
}
});
2.2 跳轉(zhuǎn)到選擇文件的頁(yè)面
//聲明文件路徑接收回調(diào)變量
private ValueCallback valueCallback;
//跳轉(zhuǎn)到選擇文件的頁(yè)面
public void getFile(ValueCallback valueCallback) {
//接收文件路徑的回調(diào)
this.valueCallback = valueCallback;
Intent intent;
//可以自己定義要跳轉(zhuǎn)的文件系統(tǒng)頁(yè)面
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宾巍、文件取消選擇后再次點(diǎn)擊無(wú)響應(yīng)
重寫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;
}
}
}
}