我正在尝试打开闪光灯 LED 一段时间,但我的代码没有按预期工作:
if(context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH))
{
Log.i("Flash Present", "Yes");
//Camera Has Flash
final Camera cam = Camera.open();
Parameters p = cam.getParameters();
p.setFlashMode(Parameters.FLASH_MODE_TORCH);
cam.setParameters(p);
ExecutorService service = Executors.newSingleThreadExecutor();
try {
Runnable r = new Runnable() {
@Override
public void run() {
Log.i("Starting Flash", "Now");
cam.startPreview();
}
};
Future<?> f = service.submit(r);
f.get(10, TimeUnit.SECONDS); // attempt the task for two minutes
}
catch (final InterruptedException e) {
// The thread was interrupted during sleep, wait or join
}
catch (final TimeoutException e) {
// Took too long!
cam.stopPreview();
cam.release();
}
catch (final ExecutionException e) {
// An exception from within the Runnable task
}
finally {
cam.stopPreview();
cam.release();
service.shutdown();
}
}
LED 不会在 10 秒后关闭,并且当再次调用此函数时会引发异常,指出我的相机资源仍在使用中并且不是空闲的。