1

我在做一个uvc摄像头功能,在android10.0系统下可以正常请求权限并使用,但是在android10.0系统下直接收到权限请求失败,并没有弹出权限请求提示弹窗,我还尝试将意图过滤器和元数据添加到清单文件的活动标记中。插入USB时,我直接选择了相关应用并授予权限,但是USB摄像头仍然无法正常工作,代码如下

<!--    AndroidManifrest.xml-->
<uses-feature android:name="android.hardware.usb.host" />

<!--    Activity-->
public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";
    //定义广播标识
    private static final String ACTION_USB_PERMISSION = "com.tangtang.uvctest.USB_PERMISSION";
    //注册接收广播 获取到usb 权限时系统广播
    private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {

        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (ACTION_USB_PERMISSION.equals(action)) {
                synchronized (this) {
                    UsbDevice device = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);

                    if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
                        if (device != null) {
                            //成功
                            toastMsg("permission succ for device " + device);
                        }
                    } else {
                        //The code will execute directly when requesting permissions
                        toastMsg("permission denied for device " + device);
                    }
                }
            }
        }

    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (!ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA)){
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA},0);
        }
        findViewById(R.id.test).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                requestPermission1();
            }
        });
    }

    private void requestPermission1() {
        //获取service
        UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
        //获取设备列表(一般只有一个,usb 口只有一个)
        HashMap<String, UsbDevice> deviceList = manager.getDeviceList();
        Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();

        //绑定广播
        PendingIntent mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
        IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
        //注册接收广播
        registerReceiver(mUsbReceiver, filter);
        //枚举设备
        while (deviceIterator.hasNext()) {
            UsbDevice device = deviceIterator.next();
            //请求权限
            manager.requestPermission(device, mPermissionIntent);
        }

    }

    private void toastMsg(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mUsbReceiver);
    }
}

4

2 回答 2

0

这已针对我的三星 note10 plus 5G 智能手机进行了软件更新。Android 已通过软件更新发布了此修复程序。(如果您的设备收到该更新,则取决于设备制造商)。查看此 google 问题以获取更多详细信息https://issuetracker.google.com/issues/145082934

于 2021-07-13T20:31:47.727 回答
0

面向 sdk 版本 > 27 时需要CAMERA权限

您可以尝试使用 saki 的 UVCPermissionTest 应用程序是否可以获得 USB 相机的权限。 https://github.com/saki4510t/UVCPermissionTest

于 2020-12-11T14:52:18.530 回答