如何检测前置摄像头在Android中是否可用?

时间:2018-09-03 09:41:29

标签: android camera crash

实际上我的应用程序出现了一些问题

就像我有一个按钮可以打开前置摄像头并打一个服务员徽章,但是当我尝试检查某个设备是否装有摄像头,是否只是隐藏该按钮时,问题就来了。

但是我有一台使用前置摄像头作为QR扫描仪的设备,因此它并不是真正可以使用的摄像头,因此当我尝试单击该按钮时,应用程序崩溃。

所以问题是我该如何处理和检查相机是否不仅存在,甚至可以工作?

2 个答案:

答案 0 :(得分:3)

使用此方法来检测设备是否具有前置摄像头。

// Save the custom fields data when submitted for product bulk edit
add_action('woocommerce_product_bulk_edit_save', 'save_custom_field_product_bulk_edit', 10, 1);
function save_custom_field_product_bulk_edit( $product ){
if ( $product->is_type('simple') || $product->is_type('external') ){
    $product_id = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id;

    if ( isset( $_REQUEST['test_column'] ) )
        update_post_meta( $product_id, '_text_field', sanitize_text_field( $_REQUEST['test_column'] ) );

    if ( isset( $_REQUEST['test_column2'] ) )
        update_post_meta( $product_id, '_select', sanitize_text_field( $_REQUEST['test_column2'] ) );
}
}

答案 1 :(得分:1)

Android Pie添加了更多features用于枚举相机:

  

在Android 9设备上运行的应用可以通过调用getCameraIdList()来发现每个可用的摄像头。应用程序不应假定该设备只有一个后置摄像头或只有一个前置摄像头。

     

例如,如果您的应用程序有一个按钮可以在前置和后置摄像头之间切换,则可能有多个前置或后置摄像头可供选择。您应该浏览摄像机列表,检查每个摄像机的特性,并确定向用户展示哪些摄像机。