WebView加载url,读取本地文件
2018-10-25 本文已影响0人
smallestt
webView.loadUrl(url);
webView.setWebChromeClient(new WebChromeClient() {
@Override
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) {
return super.onShowFileChooser(webView, filePathCallback, fileChooserParams);
if (uploadMessage != null) {
uploadMessage.onReceiveValue(null);
uploadMessage = null;
}
uploadMessage = filePathCallback;
Intent intent = fileChooserParams.createIntent();
try {
startActivityForResult(intent, REQUEST_FILE_CODE);
} catch (ActivityNotFoundException e) {
uploadMessage = null;
Toast.makeText(WebViewAct.this, "Cannot open file chooser", Toast.LENGTH_LONG).show();
return false;
}
return true;
}
});
public ValueCallback<Uri[]> uploadMessage;
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_FILE_CODE) {
if (uploadMessage == null) return;
uploadMessage.onReceiveValue(WebChromeClient.FileChooserParams.parseResult(resultCode, data));
uploadMessage = null;
}
}