2013-05-18 132 views
0

以及我正在使用服務在上&關閃光燈的相機,它工作正常,但我的手機相機應用程序崩潰,我無法釋放相機服務如何在服務中打開和關閉閃光燈相機

Camera camera = Camera.open(); 
final Parameters p = camera.getParameters();  
      if (isFlashOn) 
      { 
       Log.i("info", "torch is turned off!"); 

      Toast toast= Toast.makeText(getApplicationContext(), 
         "Torch is turned off!",Toast.LENGTH_SHORT); 
      toast.setGravity(Gravity.TOP|Gravity.CENTER_HORIZONTAL, 0, 100); 
      toast.show(); 

      p.setFlashMode(Parameters.FLASH_MODE_OFF); 
      camera.setParameters(p);   

       //Set flag to false    
      isFlashOn = false; 
      release=true; 

      } 
      //If Flag is set to false 
      else 
      { 
       Log.i("info", "torch is turned on!"); 

      Toast toast= Toast.makeText(getApplicationContext(), 
         "Torch is turned on!",Toast.LENGTH_SHORT); 
       toast.setGravity(Gravity.TOP|Gravity.CENTER_HORIZONTAL, 0, 100); 
       toast.show(); 

       p.setFlashMode(Parameters.FLASH_MODE_TORCH); 

      camera.setParameters(p); 

      isFlashOn = true; 
      release=false; 

      } 

     if (release == true) 
      { 
      camera.stopPreview(); 
      camera.release(); 
        } 

回答

1

您必須安全地打開相機,以便您必須在直接打開相機之前檢查相機狀態。

private boolean safeCameraOpen(int id) { 
    boolean qOpened = false; 

    try { 
     releaseCameraAndPreview(); 
     mCamera = Camera.open(id); 
     qOpened = (mCamera != null); 
    } catch (Exception e) { 
     Log.e(getString(R.string.app_name), "failed to open Camera"); 
     e.printStackTrace(); 
    } 

    return qOpened;  
} 

private void releaseCameraAndPreview() { 
    mPreview.setCamera(null); 
    if (mCamera != null) { 
     mCamera.release(); 
     mCamera = null; 
    } 
} 

For Further Details about Camera檢查出來並根據您的需要進行調整。

Enjoy !!!

+1

謝謝兄弟,它的工作原理。 – Uday

+0

Enjoy.if是好的,那麼不要忘記接受和upvote :) –

+0

好吧你不能投票,因爲低信譽,但你可以接受它 –

相關問題