在处理相机相关操作时,可能会遇到“android.hardware.camera2.CameraAccessException: CAMERA_DISCONNECTED (2): 相机设备已断开连接”异常。这个异常表示相机设备在操作时已经断开连接。下面是一个示例的解决方法:
import android.Manifest;
import android.content.pm.PackageManager;
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraCaptureSession;
import android.hardware.camera2.CameraDevice;
import android.hardware.camera2.CameraManager;
import android.os.Bundle;
import android.util.Log;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import java.util.Collections;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "CameraExample";
private static final int REQUEST_CAMERA_PERMISSION = 200;
private SurfaceView surfaceView;
private CameraManager cameraManager;
private String cameraId;
private CameraDevice cameraDevice;
private CameraCaptureSession cameraCaptureSession;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
surfaceView = findViewById(R.id.surfaceView);
cameraManager = (CameraManager) getSystemService(CAMERA_SERVICE);
// 检查相机权限
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, REQUEST_CAMERA_PERMISSION);
} else {
openCamera();
}
surfaceView.getHolder().addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(@NonNull SurfaceHolder surfaceHolder) {
// 创建 Surface 后,重新打开相机
if (cameraDevice != null) {
closeCamera();
}
openCamera();
}
@Override
public void surfaceChanged(@NonNull SurfaceHolder surfaceHolder, int i, int i1, int i2) {
// Surface 发生变化时,重新配置相机
configureCamera();
}
@Override
public void surfaceDestroyed(@NonNull SurfaceHolder surfaceHolder) {
// 销毁 Surface 时,关闭相机
closeCamera();
}
});
}
private void openCamera() {
try {
cameraId = cameraManager.getCameraIdList()[0];
cameraManager.openCamera(cameraId, new CameraDevice.StateCallback() {
@Override
public void onOpened(@NonNull CameraDevice cameraDevice) {
MainActivity.this.cameraDevice = cameraDevice;
configureCamera();
}
@Override
public void onDisconnected(@NonNull CameraDevice cameraDevice) {
cameraDevice.close();
MainActivity.this.cameraDevice = null;
}
@Override
public void onError(@NonNull CameraDevice cameraDevice, int i) {
cameraDevice.close();
MainActivity.this.cameraDevice = null;
}
}, null);
} catch (CameraAccessException e) {
Log.e(TAG, "相机访问异常: " + e.getMessage());
}
}
private void closeCamera() {
if (cameraCaptureSession != null) {
cameraCaptureSession.close();
cameraCaptureSession = null;
}
if (cameraDevice != null) {
cameraDevice.close();
cameraDevice = null;
}
}
private void configureCamera() {
if (cameraDevice == null) {
return;
}
try {
Surface surface = surfaceView.getHolder().getSurface();
cameraDevice.createCaptureSession(Collections.singletonList(surface), new CameraCaptureSession.StateCallback() {
@Override
public void onConfigured(@NonNull CameraCaptureSession cameraCaptureSession) {
MainActivity.this.cameraCaptureSession = cameraCaptureSession;
}
@Override
public void onConfigureFailed(@NonNull CameraCaptureSession cameraCaptureSession) {
Log.e(TAG, "相机配置失败");
}
}, null);
} catch (CameraAccessException e) {
Log.e(TAG, "相机访问异常: " + e.getMessage());
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (requestCode == REQUEST_CAMERA_PERMISSION) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
openCamera();
} else {
Log.e(TAG, "相机权限被拒绝");
}
}
}
}
在上面的示例代码中,我们使用了CameraManager
来获取相机设备,并通过cameraManager.openCamera()
方法来打开相机。