Android DWebView H5調(diào)用手機(jī)相機(jī)無反應(yīng)

解決方案:
項(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
            }
        }
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容