林製作一個應用程序對於打開/關閉LED在手機的我能理解什麼即時做錯了,我得到這個錯誤釋放後調用的方法()相機錯誤?
E/AndroidRuntime: FATAL EXCEPTION: Thread-42530
java.lang.RuntimeException: Method called after release()
at android.hardware.Camera.startPreview(Native Method)
at com.mashi.flashlight.FlashLightCallingService.flashOn(FlashLightCallingService.java:104)
at com.mashi.flashlight.FlashLightCallingService$1.run(FlashLightCallingService.java:45)
at java.lang.Thread.run(Thread.java:856)
我的代碼是
public class FlashLightCallingService extends Service {
Boolean hasFlash;
Boolean isFlashOn=true ;
Camera camera;
Camera.Parameters parameters;
@Override
public void onCreate() {
hasFlash = getApplicationContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
getCamera();
if (hasFlash)
{
new Thread(new Runnable(){
public void run() {
// TODO Auto-generated method stub
while(isFlashOn)
{
flashOn();
//flashOff();
}
}
}).start();
}
else
{
System.exit(1);
}
return START_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onDestroy() {
super.onDestroy();
isFlashOn=false;
if (camera != null) {
camera.stopPreview();
camera.release();
camera = null;
}
}
public void getCamera(){
if (camera==null)
{
try {
camera=Camera.open();
parameters=camera.getParameters();
}catch (Exception e)
{
e.printStackTrace();
}
}
}
public void flashOn(){
parameters = camera.getParameters();
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
camera.setParameters(parameters);
camera.startPreview();
}
public void flashOff(){
parameters = camera.getParameters();
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
camera.setParameters(parameters);
camera.stopPreview();
}
}
抱歉語言但英語不是我的語言
我已經做只是使用ansynctask和更良好或與此代碼就在我設置isFlashOn =假做線程。睡覺(250)和其他東西在if循環中獲得threade如果(isFlashOn)我已經完成了這個工作並且對我有效,也許有人像我一樣有相同的問題 –