首先,确保已经允许应用程序的文件管理器打开。此外,您需要使用 onActivityResult() 方法获取返回的结果。
以下是一个示例代码:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == REQUEST_SELECT_FILE) {
if (mUploadMessage == null) {
return;
}
Uri result = data == null || resultCode != RESULT_OK ? null : data.getData();
mUploadMessage.onReceiveValue(result);
mUploadMessage = null;
}
} else if (resultCode == RESULT_CANCELED) {
if (mUploadMessage != null) {
mUploadMessage.onReceiveValue(null);
mUploadMessage = null;
}
}
}
@Override
public boolean onShowFileChooser(
WebView webView, ValueCallback filePathCallback,
FileChooserParams fileChooserParams) {
if (mUploadMessage != null) {
mUploadMessage.onReceiveValue(null);
}
mUploadMessage = filePathCallback;
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("*/*");
startActivityForResult(Intent.createChooser(i, "File Chooser"), 1);
return true;
}
这样,就可以在 Android WebView 中正确显示文件选择器的返回结果了。