确定应用程序具有读取和写入存储的权限。
使用以下代码启用WebSettings:
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setAllowFileAccess(true);
webView.getSettings().setAllowFileAccessFromFileURLs(true);
webView.getSettings().setAllowUniversalAccessFromFileURLs(true);
@Override
public boolean onShowFileChooser(
WebView webView, ValueCallback filePathCallback,
WebChromeClient.FileChooserParams fileChooserParams) {
if (mFilePathCallback != null) {
mFilePathCallback.onReceiveValue(null);
}
mFilePathCallback = filePathCallback;
Intent intent = fileChooserParams.createIntent();
try {
startActivityForResult(intent, REQUEST_SELECT_FILE);
} catch (ActivityNotFoundException e) {
mFilePathCallback = null;
Toast.makeText(getActivity(), "Cannot open file chooser", Toast.LENGTH_LONG).show();
return false;
}
return true;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_SELECT_FILE) {
if (mFilePathCallback == null) return;
Uri[] result = null;
if (resultCode == RESULT_OK && data != null) {
String dataString = data.getDataString();
if (dataString != null) {
result = new Uri[]{Uri.parse(dataString)};
}
}
mFilePathCallback.onReceiveValue(result);
mFilePathCallback = null;
}
}
通过这些步骤,您应该能够让Android WebView文件上传选项正常工作。