无法获得相机运行状态

时间:2012-09-25 12:17:47

标签: android camera

检查相机是否正在运行我正在编写这些代码

ActivityManager actvityManager = (ActivityManager)this.getSystemService(ACTIVITY_SERVICE);
    String packageName = actvityManager.getRunningTasks(1).get(0).topActivity.getPackageName();
    if(packageName.equals("android.hardware.camera")||packageName.equals("com.android.camera")){
        Camera_status = "STATUS_ON";
        System.out.println("===on===");
    }else{
        Camera_status = "STATUS_OFF";
        System.out.println("====off====");
    }

通过使用这个,我能够在Android模拟器中获得正确的结果,但在测试在真实设备中我无法得到正确的结果。根据CommonsWare发布的here的答案,我认为他是所以朋友们可能知道如何以编程方式开启/关闭相机状态。

1 个答案:

答案 0 :(得分:2)

试试这个工作!!!

Camera _camera;
    boolean qOpened = false;
    try {
        _camera=Camera.open();
        qOpened=(_camera!=null);
        if(qOpened){
            Camera_status = "STATUS_OFF";
        }else{
            System.out.println("==nothing to do====");
        }

    } catch (Exception e) {
        Camera_status = "STATUS_ON";
        System.out.println("=====Camera running=====");
    }