Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public class HACWebChromeClient extends WebChromeClient {
ValueCallback<Uri[]> _filePathCallback; // ChromeClient的文件选择器回调

ActivityResultLauncher<String> _contentChooser; // 选择文件的调用器
ActivityResultLauncher<String[]> _multiFileChooser; // 多选文件的调用器
ActivityResultLauncher<Intent> _imageCaptureChooser; // 拍摄照片的调用器

static final int REQUEST_CODE_PICK_PHOTO_VIDEO = 20001; // 选取照片和视频的标识
Expand Down Expand Up @@ -164,6 +165,8 @@ public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> 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/*")) {

// 需要调用摄像头,则先申请权限
Expand All @@ -174,7 +177,7 @@ public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathC

if (Arrays.asList(fileChooserParams.getAcceptTypes()).contains("image/*")) {

// 兼容活字格官方Vant插件,支持直接调出摄像头拍照上传功能
// 兼容活字格官方Vant插件,支持"直接调出摄像头拍照上传"功能
if (fileChooserParams.isCaptureEnabled()) {

Intent cameraIntent = new Intent(this._context, CameraViewActivity.class);
Expand All @@ -188,7 +191,7 @@ public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> 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);
Expand All @@ -202,16 +205,20 @@ public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> 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);
}
});
} else {

// 直接使用系统文件选择机制
_contentChooser.launch("*/*");
// 根据多选模式选择不同的文件选择器
if (isMultiSelect) {
_multiFileChooser.launch(new String[]{"*/*"});
} else {
_contentChooser.launch("*/*");
}
}

// 标记为已经处理
Expand Down Expand Up @@ -241,7 +248,7 @@ public void registryLaunchersOnCreated() {
_filePathCallback.onReceiveValue(selectedFiles.toArray(new Uri[0]));
});

// 创建文件选择页面启动器
// 创建文件选择页面启动器(单选)
_contentChooser = _context.registerForActivityResult(new ActivityResultContracts.GetContent(),
uri -> {

Expand All @@ -256,6 +263,21 @@ public void registryLaunchersOnCreated() {
// 让页面接手处理,每一个ChooseFile都需要有配套的onReceiveValue事件
_filePathCallback.onReceiveValue(selectedFiles.toArray(new Uri[0]));
});

// 创建多选文件选择页面启动器
_multiFileChooser = _context.registerForActivityResult(new ActivityResultContracts.OpenMultipleDocuments(),
uris -> {
List<Uri> selectedFiles = new ArrayList<>();

if (uris != null) {
for (Uri uri : uris) {
selectedFiles.add(uri);
DeviceUtility.registryLatestFile(uri);
}
}

_filePathCallback.onReceiveValue(selectedFiles.toArray(new Uri[0]));
});
}

/**
Expand Down