方法如下:
在自定义的WebChromeClient中实现如下方法(主要是为了保证不同android版本上的兼容性):
public void openFileChooser(final ValueCallback<Uri> uploadMsg)
public void openFileChooser(ValueCallback uploadMsg, String acceptType)
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture)
并配置proguard保证其不被混淆掉。
在openFileChooser中使用如下Intent调用系统文件浏览器:
private Intent createDefaultOpenableIntent(final Context ctx) {
// Create and return a chooser with the default OPENABLE
// actions including the camera, camcorder and sound
// recorder where available.
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("*/*");
Intent chooser = createChooserIntent(ctx, createCameraIntent(), createCamcorderIntent(),
createSoundRecorderIntent());
chooser.putExtra(Intent.EXTRA_INTENT, i);
return chooser;
}
然后:
startActivityForResult()
在主Activity的onActivityResult()中
调用ValueCallback
但是问题还是来了,这样做了之后在4.4系统上WebView不会回调openFileChooser方法导致没法显示出选择文件的Activity。
另外对于选择文件的应用也不能假定所有的Android系统上都有。
所以终极解决方案就是拥有自主可控的WebView。
标题:WebView中支持input type="file"元素
作者:faywong8888
地址:https://blog.fay.wang/articles/2025/08/10/1754806336521.html