从后置摄像头切换时,前置摄像头无法打开

时间:2014-09-01 14:13:55

标签: android android-camera commonsware-cwac

按下按钮,我试图在后置摄像头和前置摄像头之间切换。但是当我尝试切换到前置摄像头时,它无法打开并调用onCameraFail()

然而,这只发生在第一次按下。

它可以在任何后续印刷机上正常工作。

private CameraFragment bfc, ffc, current;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_camera);

    bfc = (CameraFragment) getSupportFragmentManager()
            .findFragmentById(R.id.camera_preview);

    if (Camera.getNumberOfCameras() > 1) {
        ffc = new CustomCameraFragment(true);
        View switchCamera = findViewById(R.id.switch_camera);
        switchCamera.setVisibility(View.VISIBLE);
        switchCamera.setOnClickListener(this);
    }

    current = bfc;
}

@Override
public void onClick (View v) {
    switch (v.getId()) {
        case R.id.switch_camera:
            FragmentManager fm = getSupportFragmentManager();
            current =  (current == bfc) ? ffc : bfc;
            fm.beginTransaction().replace(R.id.camera_preview, current).commit();
            break;
    }
}

0 个答案:

没有答案