解決方案:
項(xiàng)目引用的是
implementation 'com.github.wendux:DSBridge-Android:3.0.0'
DWebView的設(shè)置為
dWebView.settings.apply {
// 訪問Content Provider的資源
allowContentAccess = true
// 訪問本地文件
allowFileAccess = true
// 設(shè)置適應(yīng)Html5
domStorageEnabled = true
// 設(shè)置允許JS彈窗
javaScriptCanOpenWindowsAutomatically = true
useWideViewPort = true
// 是否允許通過file url加載的Javascript讀取本地文件,默認(rèn)值 false
allowFileAccessFromFileURLs = true
// 是否允許通過file url加載的Javascript讀取全部資源(包括文件,http,https),默認(rèn)值 false
allowUniversalAccessFromFileURLs = true
//開啟JavaScript支持
javaScriptEnabled = true
}
重點(diǎn)在 WebChromeClient ,WebView同理,不設(shè)置不會(huì)調(diào)用本地相機(jī)
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
// 創(chuàng)建拍照的 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
}
}