Android DWebView H5调用手机相机无反应

解决方案:
项目引用的是

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
            }
        }
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容