From 8c40aced23a75d3d7124d7f224aa8874ff5952a1 Mon Sep 17 00:00:00 2001 From: hanzhou Date: Mon, 23 Mar 2026 17:12:08 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E6=96=87=E4=BB=B6=E5=A4=9A?= =?UTF-8?q?=E9=80=89=E9=97=AE=E9=A2=98=EF=BC=9A=E6=94=AF=E6=8C=81WebView?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E9=80=89=E6=8B=A9=E5=99=A8=E7=9A=84=E5=A4=9A?= =?UTF-8?q?=E9=80=89=E6=A8=A1=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../container/webview/HACWebChromeClient.java | 34 +++++++++++++++---- 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/com/huozige/lab/container/webview/HACWebChromeClient.java b/app/src/main/java/com/huozige/lab/container/webview/HACWebChromeClient.java index 07c3a287..db77b18a 100644 --- a/app/src/main/java/com/huozige/lab/container/webview/HACWebChromeClient.java +++ b/app/src/main/java/com/huozige/lab/container/webview/HACWebChromeClient.java @@ -46,6 +46,7 @@ public class HACWebChromeClient extends WebChromeClient { ValueCallback _filePathCallback; // ChromeClient的文件选择器回调 ActivityResultLauncher _contentChooser; // 选择文件的调用器 + ActivityResultLauncher _multiFileChooser; // 多选文件的调用器 ActivityResultLauncher _imageCaptureChooser; // 拍摄照片的调用器 static final int REQUEST_CODE_PICK_PHOTO_VIDEO = 20001; // 选取照片和视频的标识 @@ -164,6 +165,8 @@ public boolean onShowFileChooser(WebView webView, ValueCallback filePathC _filePathCallback = filePathCallback; // 将参数缓存起来,回调使用 + boolean isMultiSelect = fileChooserParams.getMode() == WebChromeClient.FileChooserParams.MODE_OPEN_MULTIPLE; + if (Arrays.asList(fileChooserParams.getAcceptTypes()).contains("video/*") || Arrays.asList(fileChooserParams.getAcceptTypes()).contains("image/*")) { // 需要调用摄像头,则先申请权限 @@ -174,7 +177,7 @@ public boolean onShowFileChooser(WebView webView, ValueCallback filePathC if (Arrays.asList(fileChooserParams.getAcceptTypes()).contains("image/*")) { - // 兼容活字格官方Vant插件,支持“直接调出摄像头拍照上传”功能 + // 兼容活字格官方Vant插件,支持"直接调出摄像头拍照上传"功能 if (fileChooserParams.isCaptureEnabled()) { Intent cameraIntent = new Intent(this._context, CameraViewActivity.class); @@ -188,7 +191,7 @@ public boolean onShowFileChooser(WebView webView, ValueCallback filePathC .capture(true) .captureStrategy(//参数1 true表示拍照存储在共有目录,false表示存储在私有目录;参数2与 AndroidManifest中authorities值相同,用于适配7.0系统 必须设置 new CaptureStrategy(true, "com.huozige.lab.container")) - .maxSelectable(1) + .maxSelectable(isMultiSelect ? 99 : 1) .thumbnailScale(0.85f) .imageEngine(new GlideEngine()) .forResult(REQUEST_CODE_PICK_PHOTO_VIDEO); @@ -202,7 +205,7 @@ public boolean onShowFileChooser(WebView webView, ValueCallback filePathC .capture(true) .captureStrategy(//参数1 true表示拍照存储在共有目录,false表示存储在私有目录;参数2与 AndroidManifest中authorities值相同,用于适配7.0系统 必须设置 new CaptureStrategy(true, "com.huozige.lab.container")) - .maxSelectable(1) + .maxSelectable(isMultiSelect ? 99 : 1) .thumbnailScale(0.85f) .imageEngine(new GlideEngine()) .forResult(REQUEST_CODE_PICK_PHOTO_VIDEO); @@ -210,8 +213,12 @@ public boolean onShowFileChooser(WebView webView, ValueCallback filePathC }); } else { - // 直接使用系统文件选择机制 - _contentChooser.launch("*/*"); + // 根据多选模式选择不同的文件选择器 + if (isMultiSelect) { + _multiFileChooser.launch(new String[]{"*/*"}); + } else { + _contentChooser.launch("*/*"); + } } // 标记为已经处理 @@ -241,7 +248,7 @@ public void registryLaunchersOnCreated() { _filePathCallback.onReceiveValue(selectedFiles.toArray(new Uri[0])); }); - // 创建文件选择页面启动器 + // 创建文件选择页面启动器(单选) _contentChooser = _context.registerForActivityResult(new ActivityResultContracts.GetContent(), uri -> { @@ -256,6 +263,21 @@ public void registryLaunchersOnCreated() { // 让页面接手处理,每一个ChooseFile都需要有配套的onReceiveValue事件 _filePathCallback.onReceiveValue(selectedFiles.toArray(new Uri[0])); }); + + // 创建多选文件选择页面启动器 + _multiFileChooser = _context.registerForActivityResult(new ActivityResultContracts.OpenMultipleDocuments(), + uris -> { + List selectedFiles = new ArrayList<>(); + + if (uris != null) { + for (Uri uri : uris) { + selectedFiles.add(uri); + DeviceUtility.registryLatestFile(uri); + } + } + + _filePathCallback.onReceiveValue(selectedFiles.toArray(new Uri[0])); + }); } /**