解决方案:
项目引用的是
implementation 'com.github.wendux:DSBridge-Android:3.0.0'
DWebView的设置为
dWebView.settings.apply {
// 访问Content Provider的资源
allowContentAccess = true
// 访问本地文件
allowFileAccess = true
// 设置适应Html5
domStorageEnabled = true
// 设置允许JS弹窗
javaScriptCanOpenWindowsAutomatically = true
useWideViewPort = true
// 是否允许通过file url加载的Javascript读取本地文件,默认值 false
allowFileAccessFromFileURLs = true
// 是否允许通过file url加载的Javascript读取全部资源(包括文件,http,https),默认值 false
allowUniversalAccessFromFileURLs = true
//开启JavaScript支持
javaScriptEnabled = true
}
重点在 WebChromeClient ,WebView同理,不设置不会调用本地相机
private var filePathCallback: ValueCallback<Array<Uri>>? = null
private var photoFile: File? = null
dWebView.webChromeClient = object: WebChromeClient() {
override fun onShowFileChooser(
webView: WebView?,
filePathCallback: ValueCallback<Array<Uri>>?,
fileChooserParams: FileChooserParams?
): Boolean {
this@MainActivity.filePathCallback = filePathCallback
// 创建拍照的 Intent
val takePictureIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
if (takePictureIntent.resolveActivity(packageManager) != null) {
try {
photoFile = createImageFile()
if (photoFile != null) {
val imageUri = getUriForFile(photoFile!!)
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri)
takePictureLauncher.launch(takePictureIntent)
}
} catch (ex: IOException) {
ex.printStackTrace()
}
}
return true
}
}