作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我们连接到 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/
我是一名优秀的程序员,十分优秀!