我找到了自己的答案。final Camera mCamera=Camera.open();有一个未检查的RunTimeException,所以我只是用try-catch处理了这个异常,它修复了它
我想让相机发光。我的代码会让它只需按下一个按钮就可以闪烁,但如果我在它完成闪烁之前再次按下按钮,它会使我的应用程序崩溃。我想我需要先检查一下它是否还在闪烁,然后再尝试重新启动闪光灯。
public class BlinkBack {
public static void blink(MainActivity ma){
if (ma.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)){
final Camera mCamera = Camera.open();
new CountDownTimer(5000, 100) {
int counter = 0;
public void onTick(long millisUntilFinished) {
if (counter % 2 == 0) {
Camera.Parameters params = mCamera.getParameters();
params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
mCamera.setParameters(params);
mCamera.startPreview();
}
else {
Camera.Parameters params = mCamera.getParameters();
params.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
mCamera.setParameters(params);
mCamera.stopPreview();
}
counter++;
}
public void onFinish() {
Camera.Parameters params = mCamera.getParameters();
params.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
mCamera.setParameters(params);
mCamera.stopPreview();
mCamera.release();
}
}.start();
}
}
}
public class BlinkBack {
public static void blink(MainActivity ma){
if (ma.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)){
try {
final Camera mCamera = Camera.open();
new CountDownTimer(5000, 100) {
int counter = 0;
public void onTick(long millisUntilFinished) {
if (counter % 2 == 0) {
Camera.Parameters params = mCamera.getParameters();
params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
mCamera.setParameters(params);
mCamera.startPreview();
}
else {
Camera.Parameters params = mCamera.getParameters();
params.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
mCamera.setParameters(params);
mCamera.stopPreview();
}
counter++;
}
public void onFinish() {
Camera.Parameters params = mCamera.getParameters();
params.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
mCamera.setParameters(params);
mCamera.stopPreview();
mCamera.release();
}
}.start();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
}