webView上传文件就不多说,上代码
webview.setWebChromeClient(new WebChromeClient() {
// For Android < 3.0
public void openFileChooser(ValueCallback<Uri> valueCallback) {
***
}
// For Android >= 3.0
public void openFileChooser(ValueCallback valueCallback, String acceptType) {
***
}
//For Android >= 4.1
public void openFileChooser(ValueCallback<Uri> valueCallback,
String acceptType, String capture) {
***
}
// For Android >= 5.0
@Override
public boolean onShowFileChooser(WebView webView,
ValueCallback<Uri[]> filePathCallback,
WebChromeClient.FileChooserParams fileChooserParams) {
***
return true;
}
});
文件选择完成之后的回调,此处是上传照片的例子
/***
* 文件选择的回调
* @param list
*/
public void setChooseFile(ArrayList<TImage> list) {
KLog.e(TAG, "webfragment中回调了图片:" + list.size());
Uri[] results = null;
Uri uriResult = null;
if (null != list && list.size() > 0) {
TImage tImage = list.get(0);
//取到压缩后的图
String compressPath = tImage.getCompressPath();
results = new Uri[]{Uri.fromFile(new File(compressPath))};
uriResult = Uri.fromFile(new File(compressPath));
}
if (null != uploadMessageAboveL) {
uploadMessageAboveL.onReceiveValue(results);
} else if (null != mValueCallback) {
mValueCallback.onReceiveValue(uriResult);
}
}
但是有时候我们点击了上传按钮,但是没有上传任何文件就退出了,这时候就会出现再次点击上传按钮,不显示让选择文件的弹窗(比如上传照片时,不显示从相册选取还是拍照获取的dialog),解决办法如下:
将选择文件的回调全部设置成null
/***
* 文件选择弹框取消时回调
* 解决弹框二次不弹出的问题
*/
public void cancleChooseFileDialog() {
if (null != uploadMessageAboveL) {
uploadMessageAboveL.onReceiveValue(null);
uploadMessageAboveL = null;
}
if (null != mValueCallback) {
mValueCallback.onReceiveValue(null);
mValueCallback = null;
}
}
这个方法,我们在dialog的cancle回调里面去调用,另外,记得将这个dialog设置成只能点击cancle取消的模式,否则会出现点击dialog以外的部分,dialog消失,但是这个回调没有执行的情况
网友评论