权限有了
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.FLASHLIGHT" />
<!-- 调用硬件相机权限 -->
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
@Override
public void surfaceCreated(SurfaceHolder holder) {
if (!hasSurface) {
hasSurface = true;
initCamera(holder);
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
hasSurface = false;
if (camera != null) {
if (CameraManager.get().isPreviewing()) {
if (!CameraManager.get().isUseOneShotPreviewCallback()) {
camera.setPreviewCallback(null);
}
camera.stopPreview();
camera.release();
camera = null;
CameraManager.get().getPreviewCallback().setHandler(null, 0);
CameraManager.get().getAutoFocusCallback().setHandler(null, 0);
CameraManager.get().setPreviewing(false);
}
}
}
private void initCamera(SurfaceHolder surfaceHolder) {
try {
//6.0+调用相机权限
if (ContextCompat.checkSelfPermission(getContext(), Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions((Activity) getContext(),
new String[]{Manifest.permission.CAMERA},
TAKE_PHOTO_REQUEST_CODE);
}else {
camera = Camera.open();
parameter = camera.getParameters();
CameraManager.get().openDriver(surfaceHolder);
camera = CameraManager.get().getCamera();
camera.startPreview();
}
} catch (IOException | RuntimeException e) {
L.e(e.getMessage());
return;
}
if (handler == null) {
handler = new CaptureActivityHandler(this, decodeFormats, characterSet, viewfinderView);
}
}
请问少了什么,为什么会失败?
在6.0上测试没问题,换了红米2A 4.4的系统就不行了。
求解
谢谢~