gpt4 book ai didi

android - 如何从 Webview 中访问摄像头?

转载 作者:行者123 更新时间:2023-12-05 09:03:54 26 4
gpt4 key购买 nike

我们连接到 https。如果我在 Android 设备上使用 chrome 浏览器,我们可以在启用相机和麦克风的情况下进行连接。也可以打开和关闭它们。但是,如果我们尝试使用 WebView 进行相同操作,我们甚至不会收到网站授权访问的提示,并且在尝试打开它们时出现“无法访问摄像头/麦克风”错误。

list

 <uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.webkit.PermissionRequest" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

WebView Activity

class WebView : AppCompatActivity() {

val permission = arrayOf(Manifest.permission.CAMERA,
Manifest.permission.RECORD_AUDIO,
Manifest.permission.MODIFY_AUDIO_SETTINGS)
val requestCode = 1
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_web_view)

WebViewSetup()


if (!isPermissionGranted()) {
askPermissions()


}


}


private fun askPermissions() {
ActivityCompat.requestPermissions(this, permission, requestCode)
}

private fun isPermissionGranted(): Boolean {
permission.forEach {
if (ActivityCompat.checkSelfPermission(this, it) != PackageManager.PERMISSION_GRANTED)
return false
}

return true
}


@SuppressLint("SetJavaScriptEnabled")
private fun WebViewSetup() {


val url = intent.extras!!.getString("url")
webview.webChromeClient = WebChromeClient()
webview.apply {
if (url != null) {
loadUrl(url)
}

Log.d("callBtn", "Url :: $url!!!! ")
settings.javaScriptEnabled = true
settings.javaScriptCanOpenWindowsAutomatically = true
settings.domStorageEnabled = true
settings.javaScriptCanOpenWindowsAutomatically = true
settings.allowContentAccess = true
settings.safeBrowsingEnabled = true
settings.mediaPlaybackRequiresUserGesture = false
}
}
}

最佳答案

这是答案

class WebViewActivity : AppCompatActivity() {

private val permission = arrayOf(Manifest.permission.CAMERA,
Manifest.permission.RECORD_AUDIO,
Manifest.permission.MODIFY_AUDIO_SETTINGS)
private val requestCode = 1

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_web_view)
val action: String? = intent?.action
val data: Uri? = intent?.data

WebViewSetup()
if (!isPermissionGranted()) {

askPermissions()

}

webview.webChromeClient = object : WebChromeClient() {
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
override fun onPermissionRequest(request: PermissionRequest) {
request.grant(request.resources)
}
}

}


private fun askPermissions() {
ActivityCompat.requestPermissions(this, permission, requestCode)
}

private fun isPermissionGranted(): Boolean {
permission.forEach {
if (ActivityCompat.checkSelfPermission(this, it) != PackageManager.PERMISSION_GRANTED)
return false
}

return true
}

@SuppressLint("SetJavaScriptEnabled")
private fun WebViewSetup() {

val url = intent.extras!!.getString("url")
webview.webChromeClient = WebChromeClient()


webview.apply {
if (url != null) {
loadUrl(url)
}

Log.d("callBtn", "Url :: $url!!!! ")
settings.javaScriptEnabled = true
settings.javaScriptCanOpenWindowsAutomatically = true
settings.domStorageEnabled = true
settings.allowContentAccess = true
settings.safeBrowsingEnabled = true
settings.mediaPlaybackRequiresUserGesture = false


}
}

}

关于android - 如何从 Webview 中访问摄像头?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69420408/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com