浏览器唤起相机失败问题

浏览器唤起相机依赖接口返回值,偶现唤起失败,报错为file chooser dialog can only be shown with a user activation。原因是vivo浏览器安全机制,长时间无响应会移除行为。解决方案是尽量改交互,不依赖接口或后置接口,防止此类情况发生。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

背景:浏览器是否可以唤起相机,依赖于接口的返回值,在此条件下,偶现唤起失败的情况,报的错误为file chooser dialog can only be shown with a user activation

原因:因为vivo浏览器安全机制原因,用户长时间操作无响应,浏览器会移除此次行为,写了个demo,发现只有情况1是可以唤起的

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>test</title>
</head>
<body>
    <button style="width: 200px;height:80px;margin-top: 100px;" id="button1">延迟 1s</button><br>
    <button style="width: 200px;height:80px;margin-top: 100px;" id="button2">延迟 2s</button><br>
    <button style="width: 200px;height:80px;margin-top: 100px;" id="button3">轮询后第二次点击无效</button><br>
    <input  style="width: 200px;height:80px;margin-top: 100px;" id="input" type="file" accept="video/*" capture="camcorder">
    <script>
        var num = 1;
        document.getElementById('button1').onclick = function(){ setTimeout(() => { document.getElementById('input').click() }, 1000) }
        document.getElementById('button2').onclick = function(){ setTimeout(() => { document.getElementById('input').click() }, 2000) }
        document.getElementById('button3').onclick = function(){ 
            var timer = setInterval(() => { 
            alert(num)
            if(num == 2) {
                document.getElementById('input').click() 
                clearInterval(timer)
            }
            num++;
        }, 500) }
    </script>
</body>
</html>

解决方案:尽量改交互吧,不依赖接口,或接口后置掉,防止类型情况发生

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值