我试图将闪光灯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();
}
}
10秒钟后LED没有关闭,当对此功能进行另一次调用时,会抛出异常,说我的相机资源仍然在使用而且不是免费的。
答案 0 :(得分:0)
好的,我自己想出了解决方案。以下是我对我的代码所做的工作。
public void NotifyWithFlash(Context context){
boolean ShouldIGlow = true;
while(ShouldIGlow){
flashON();
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
ShouldIGlow = false;
flashOFF();
}
}
}
public void flashON(){
cam = Camera.open();
Parameters p = cam.getParameters();
p.setFlashMode(Parameters.FLASH_MODE_TORCH);
cam.setParameters(p);
cam.startPreview();
}
public void flashOFF(){
cam.stopPreview();
cam.release();
}