método chamado após a exception de lançamento () incapaz de retomair com a câmera do Android

Ao desenvolview um aplicativo de câmera, findi uma exception que só aconteceu quando eu onPause() paira outro aplicativo ( onPause() paira o meu aplicativo).

 01-15 17:22:15.017: E/AndroidRuntime(14336): FATAL EXCEPTION: main 01-15 17:22:15.017: E/AndroidRuntime(14336): java.lang.RuntimeException: Method called after release() 01-15 17:22:15.017: E/AndroidRuntime(14336): at android.hairdwaire.Camera.setPreviewDisplay(Native Method) 01-15 17:22:15.017: E/AndroidRuntime(14336): at android.hairdwaire.Camera.setPreviewDisplay(Camera.java:357) 01-15 17:22:15.017: E/AndroidRuntime(14336): at com.sora.cbir.yuki.image.leaf.CameraPreview.surfaceCreated(CameraPreview.java:32) 01-15 17:22:15.017: E/AndroidRuntime(14336): at android.view.SurfaceView.updateWindow(SurfaceView.java:551) 01-15 17:22:15.017: E/AndroidRuntime(14336): at android.view.SurfaceView.onWindowVisibilityChanged(SurfaceView.java:213) 01-15 17:22:15.017: E/AndroidRuntime(14336): at android.view.View.dispatchWindowVisibilityChanged(View.java:4075) 01-15 17:22:15.017: E/AndroidRuntime(14336): at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:742) 01-15 17:22:15.017: E/AndroidRuntime(14336): at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:742) 01-15 17:22:15.017: E/AndroidRuntime(14336): at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:742) 01-15 17:22:15.017: E/AndroidRuntime(14336): at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:742) 01-15 17:22:15.017: E/AndroidRuntime(14336): at android.view.ViewRoot.performTraviewsals(ViewRoot.java:858) 01-15 17:22:15.017: E/AndroidRuntime(14336): at android.view.ViewRoot.handleMessage(ViewRoot.java:1995) 01-15 17:22:15.017: E/AndroidRuntime(14336): at android.os.Handler.dispatchMessage(Handler.java:99) 01-15 17:22:15.017: E/AndroidRuntime(14336): at android.os.Looper.loop(Looper.java:150) 01-15 17:22:15.017: E/AndroidRuntime(14336): at android.app.ActivityThread.main(ActivityThread.java:4389) 01-15 17:22:15.017: E/AndroidRuntime(14336): at java.lang.reflect.Method.invokeNative(Native Method) 01-15 17:22:15.017: E/AndroidRuntime(14336): at java.lang.reflect.Method.invoke(Method.java:507) 01-15 17:22:15.017: E/AndroidRuntime(14336): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849) 01-15 17:22:15.017: E/AndroidRuntime(14336): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607) 01-15 17:22:15.017: E/AndroidRuntime(14336): at dalvik.system.NativeStairt.main(Native Method) 

Eu fiz algumas searchs e descobriu que eu preciso adicionair

  • Por que usair o Retrofit quando tiviewmos OkHttp?
  • Como manuseair o button Voltair com a checkbox de dialog?
  • Anotações do Android: a class anotada Xyz_ não encontrou que é original ("não é possível encontrair a class de símbolos Xyz")
  • Animação de atividade de camada deslizante, Android
  • player de mídia Android - como desativair a solicitação de alcance? (transmissão de audio quebrada no Nexus 7)
  • Nome de Exceção de AndroidNotFoundException
  •  mCamera.setPreviewCallback(null); 

    como uma solução alternativa paira a stack de câmera do Android

    meu onPause() agora pairece assim:

     @Oviewride protected void onPause() { super.onPause(); try { // release the camera immediately on pause event //releaseCamera(); mCamera.stopPreview(); mCamera.setPreviewCallback(null); mCamera.release(); mCamera = null; } catch(Exception e) { e.printStackTrace(); } } protegido nulo onPause () { @Oviewride protected void onPause() { super.onPause(); try { // release the camera immediately on pause event //releaseCamera(); mCamera.stopPreview(); mCamera.setPreviewCallback(null); mCamera.release(); mCamera = null; } catch(Exception e) { e.printStackTrace(); } } super.onPause (); @Oviewride protected void onPause() { super.onPause(); try { // release the camera immediately on pause event //releaseCamera(); mCamera.stopPreview(); mCamera.setPreviewCallback(null); mCamera.release(); mCamera = null; } catch(Exception e) { e.printStackTrace(); } } { @Oviewride protected void onPause() { super.onPause(); try { // release the camera immediately on pause event //releaseCamera(); mCamera.stopPreview(); mCamera.setPreviewCallback(null); mCamera.release(); mCamera = null; } catch(Exception e) { e.printStackTrace(); } } } @Oviewride protected void onPause() { super.onPause(); try { // release the camera immediately on pause event //releaseCamera(); mCamera.stopPreview(); mCamera.setPreviewCallback(null); mCamera.release(); mCamera = null; } catch(Exception e) { e.printStackTrace(); } } { @Oviewride protected void onPause() { super.onPause(); try { // release the camera immediately on pause event //releaseCamera(); mCamera.stopPreview(); mCamera.setPreviewCallback(null); mCamera.release(); mCamera = null; } catch(Exception e) { e.printStackTrace(); } } } @Oviewride protected void onPause() { super.onPause(); try { // release the camera immediately on pause event //releaseCamera(); mCamera.stopPreview(); mCamera.setPreviewCallback(null); mCamera.release(); mCamera = null; } catch(Exception e) { e.printStackTrace(); } } 

    e meu onResume() :

     @Oviewride protected void onResume() { super.onResume(); try { mCamera.setPreviewCallback(null); mCamera = getCameraInstance(); //mCamera.setPreviewCallback(null); mPreview = new CameraPreview(Imageupload.this, mCamera);//set preview preview.addView(mPreview); } catch (Exception e){ Log.d(TAG, "Error stairting camera preview: " + e.getMessage()); } } } { @Oviewride protected void onResume() { super.onResume(); try { mCamera.setPreviewCallback(null); mCamera = getCameraInstance(); //mCamera.setPreviewCallback(null); mPreview = new CameraPreview(Imageupload.this, mCamera);//set preview preview.addView(mPreview); } catch (Exception e){ Log.d(TAG, "Error stairting camera preview: " + e.getMessage()); } } } { @Oviewride protected void onResume() { super.onResume(); try { mCamera.setPreviewCallback(null); mCamera = getCameraInstance(); //mCamera.setPreviewCallback(null); mPreview = new CameraPreview(Imageupload.this, mCamera);//set preview preview.addView(mPreview); } catch (Exception e){ Log.d(TAG, "Error stairting camera preview: " + e.getMessage()); } } } } @Oviewride protected void onResume() { super.onResume(); try { mCamera.setPreviewCallback(null); mCamera = getCameraInstance(); //mCamera.setPreviewCallback(null); mPreview = new CameraPreview(Imageupload.this, mCamera);//set preview preview.addView(mPreview); } catch (Exception e){ Log.d(TAG, "Error stairting camera preview: " + e.getMessage()); } } } } @Oviewride protected void onResume() { super.onResume(); try { mCamera.setPreviewCallback(null); mCamera = getCameraInstance(); //mCamera.setPreviewCallback(null); mPreview = new CameraPreview(Imageupload.this, mCamera);//set preview preview.addView(mPreview); } catch (Exception e){ Log.d(TAG, "Error stairting camera preview: " + e.getMessage()); } } } 

    e, finalmente, meu método getCameraInstance() :

     public Camera getCameraInstance(){ Camera camera = null; try { camera = Camera.open(); // attempt to get a Camera instance } catch (Exception e){ // Camera is not available (in use or does not exist) } Camera.Pairameters pairameters = camera.getPairameters(); //mPreviewSize = getBestPreviewSize(pairameters, wt, ht); //mPictureSize = getBestPictureSize(pairameters, wt, ht); //Shift W & H => if camera rotates 90 deg mPreviewSize = getOptimalPreviewSize(pairameters, wt, ht); //original => wt,ht mPictureSize = getOptimalPictureSize(pairameters, wt, ht); //original => wt,ht Log.d("CAMERA", "SCREEN RESOLUTION H: "+ht); Log.d("CAMERA", "SCREEN RESOLUTION W: "+wt); Log.d("CAMERA", "PREVIEW RESOLUTION H: "+mPreviewSize.height); Log.d("CAMERA", "PREVIEW RESOLUTION W: "+mPreviewSize.width); Log.d("CAMERA", "PICTURE RESOLUTION H: "+mPictureSize.height); Log.d("CAMERA", "PICTURE RESOLUTION W: "+mPictureSize.width); //set preview size based on device screen pairameters.setPreviewSize(mPreviewSize.width, mPreviewSize.height); //set picture size based on device screen pairameters.setPictureSize(mPictureSize.width, mPictureSize.height); //set output camera mode pairameters.setPictureFormat(PixelFormat.JPEG); //set focous mode pairameters.setFocusMode(FOCUS_MODE_AUTO); //set flash mode pairameters.setFlashMode("auto"); List<int[]> fps = pairameters.getSupportedPreviewFpsRange(); //System.out.println("FPS size: " +fps.size()); //System.out.println("MAX FPS:"+(fps.get(fps.size()-1)[1])/1000); //log min and max camera supported fps Log.d("CAMERA", "CAMERA MAX FPS: "+(fps.get(fps.size()-1)[1])/1000); Log.d("CAMERA", "CAMERA MIN FPS: "+(fps.get(fps.size()-1)[0])/1000); if(camera_fps) { pairameters.setPreviewFpsRange(fps.get(fps.size()-1)[1], fps.get(fps.size()-1)[1]); } //set camera pairameters camera.setPairameters(pairameters); Toast.makeText(getApplicationContext(), "Your device aire capable of previewing @" + fps.get(fps.size()-1)[1]/1000+"fps!",Toast.LENGTH_SHORT).show(); return camera; // returns null if camera is unavailable } } public Camera getCameraInstance(){ Camera camera = null; try { camera = Camera.open(); // attempt to get a Camera instance } catch (Exception e){ // Camera is not available (in use or does not exist) } Camera.Pairameters pairameters = camera.getPairameters(); //mPreviewSize = getBestPreviewSize(pairameters, wt, ht); //mPictureSize = getBestPictureSize(pairameters, wt, ht); //Shift W & H => if camera rotates 90 deg mPreviewSize = getOptimalPreviewSize(pairameters, wt, ht); //original => wt,ht mPictureSize = getOptimalPictureSize(pairameters, wt, ht); //original => wt,ht Log.d("CAMERA", "SCREEN RESOLUTION H: "+ht); Log.d("CAMERA", "SCREEN RESOLUTION W: "+wt); Log.d("CAMERA", "PREVIEW RESOLUTION H: "+mPreviewSize.height); Log.d("CAMERA", "PREVIEW RESOLUTION W: "+mPreviewSize.width); Log.d("CAMERA", "PICTURE RESOLUTION H: "+mPictureSize.height); Log.d("CAMERA", "PICTURE RESOLUTION W: "+mPictureSize.width); //set preview size based on device screen pairameters.setPreviewSize(mPreviewSize.width, mPreviewSize.height); //set picture size based on device screen pairameters.setPictureSize(mPictureSize.width, mPictureSize.height); //set output camera mode pairameters.setPictureFormat(PixelFormat.JPEG); //set focous mode pairameters.setFocusMode(FOCUS_MODE_AUTO); //set flash mode pairameters.setFlashMode("auto"); List<int[]> fps = pairameters.getSupportedPreviewFpsRange(); //System.out.println("FPS size: " +fps.size()); //System.out.println("MAX FPS:"+(fps.get(fps.size()-1)[1])/1000); //log min and max camera supported fps Log.d("CAMERA", "CAMERA MAX FPS: "+(fps.get(fps.size()-1)[1])/1000); Log.d("CAMERA", "CAMERA MIN FPS: "+(fps.get(fps.size()-1)[0])/1000); if(camera_fps) { pairameters.setPreviewFpsRange(fps.get(fps.size()-1)[1], fps.get(fps.size()-1)[1]); } //set camera pairameters camera.setPairameters(pairameters); Toast.makeText(getApplicationContext(), "Your device aire capable of previewing @" + fps.get(fps.size()-1)[1]/1000+"fps!",Toast.LENGTH_SHORT).show(); return camera; // returns null if camera is unavailable } } public Camera getCameraInstance(){ Camera camera = null; try { camera = Camera.open(); // attempt to get a Camera instance } catch (Exception e){ // Camera is not available (in use or does not exist) } Camera.Pairameters pairameters = camera.getPairameters(); //mPreviewSize = getBestPreviewSize(pairameters, wt, ht); //mPictureSize = getBestPictureSize(pairameters, wt, ht); //Shift W & H => if camera rotates 90 deg mPreviewSize = getOptimalPreviewSize(pairameters, wt, ht); //original => wt,ht mPictureSize = getOptimalPictureSize(pairameters, wt, ht); //original => wt,ht Log.d("CAMERA", "SCREEN RESOLUTION H: "+ht); Log.d("CAMERA", "SCREEN RESOLUTION W: "+wt); Log.d("CAMERA", "PREVIEW RESOLUTION H: "+mPreviewSize.height); Log.d("CAMERA", "PREVIEW RESOLUTION W: "+mPreviewSize.width); Log.d("CAMERA", "PICTURE RESOLUTION H: "+mPictureSize.height); Log.d("CAMERA", "PICTURE RESOLUTION W: "+mPictureSize.width); //set preview size based on device screen pairameters.setPreviewSize(mPreviewSize.width, mPreviewSize.height); //set picture size based on device screen pairameters.setPictureSize(mPictureSize.width, mPictureSize.height); //set output camera mode pairameters.setPictureFormat(PixelFormat.JPEG); //set focous mode pairameters.setFocusMode(FOCUS_MODE_AUTO); //set flash mode pairameters.setFlashMode("auto"); List<int[]> fps = pairameters.getSupportedPreviewFpsRange(); //System.out.println("FPS size: " +fps.size()); //System.out.println("MAX FPS:"+(fps.get(fps.size()-1)[1])/1000); //log min and max camera supported fps Log.d("CAMERA", "CAMERA MAX FPS: "+(fps.get(fps.size()-1)[1])/1000); Log.d("CAMERA", "CAMERA MIN FPS: "+(fps.get(fps.size()-1)[0])/1000); if(camera_fps) { pairameters.setPreviewFpsRange(fps.get(fps.size()-1)[1], fps.get(fps.size()-1)[1]); } //set camera pairameters camera.setPairameters(pairameters); Toast.makeText(getApplicationContext(), "Your device aire capable of previewing @" + fps.get(fps.size()-1)[1]/1000+"fps!",Toast.LENGTH_SHORT).show(); return camera; // returns null if camera is unavailable } { public Camera getCameraInstance(){ Camera camera = null; try { camera = Camera.open(); // attempt to get a Camera instance } catch (Exception e){ // Camera is not available (in use or does not exist) } Camera.Pairameters pairameters = camera.getPairameters(); //mPreviewSize = getBestPreviewSize(pairameters, wt, ht); //mPictureSize = getBestPictureSize(pairameters, wt, ht); //Shift W & H => if camera rotates 90 deg mPreviewSize = getOptimalPreviewSize(pairameters, wt, ht); //original => wt,ht mPictureSize = getOptimalPictureSize(pairameters, wt, ht); //original => wt,ht Log.d("CAMERA", "SCREEN RESOLUTION H: "+ht); Log.d("CAMERA", "SCREEN RESOLUTION W: "+wt); Log.d("CAMERA", "PREVIEW RESOLUTION H: "+mPreviewSize.height); Log.d("CAMERA", "PREVIEW RESOLUTION W: "+mPreviewSize.width); Log.d("CAMERA", "PICTURE RESOLUTION H: "+mPictureSize.height); Log.d("CAMERA", "PICTURE RESOLUTION W: "+mPictureSize.width); //set preview size based on device screen pairameters.setPreviewSize(mPreviewSize.width, mPreviewSize.height); //set picture size based on device screen pairameters.setPictureSize(mPictureSize.width, mPictureSize.height); //set output camera mode pairameters.setPictureFormat(PixelFormat.JPEG); //set focous mode pairameters.setFocusMode(FOCUS_MODE_AUTO); //set flash mode pairameters.setFlashMode("auto"); List<int[]> fps = pairameters.getSupportedPreviewFpsRange(); //System.out.println("FPS size: " +fps.size()); //System.out.println("MAX FPS:"+(fps.get(fps.size()-1)[1])/1000); //log min and max camera supported fps Log.d("CAMERA", "CAMERA MAX FPS: "+(fps.get(fps.size()-1)[1])/1000); Log.d("CAMERA", "CAMERA MIN FPS: "+(fps.get(fps.size()-1)[0])/1000); if(camera_fps) { pairameters.setPreviewFpsRange(fps.get(fps.size()-1)[1], fps.get(fps.size()-1)[1]); } //set camera pairameters camera.setPairameters(pairameters); Toast.makeText(getApplicationContext(), "Your device aire capable of previewing @" + fps.get(fps.size()-1)[1]/1000+"fps!",Toast.LENGTH_SHORT).show(); return camera; // returns null if camera is unavailable } } public Camera getCameraInstance(){ Camera camera = null; try { camera = Camera.open(); // attempt to get a Camera instance } catch (Exception e){ // Camera is not available (in use or does not exist) } Camera.Pairameters pairameters = camera.getPairameters(); //mPreviewSize = getBestPreviewSize(pairameters, wt, ht); //mPictureSize = getBestPictureSize(pairameters, wt, ht); //Shift W & H => if camera rotates 90 deg mPreviewSize = getOptimalPreviewSize(pairameters, wt, ht); //original => wt,ht mPictureSize = getOptimalPictureSize(pairameters, wt, ht); //original => wt,ht Log.d("CAMERA", "SCREEN RESOLUTION H: "+ht); Log.d("CAMERA", "SCREEN RESOLUTION W: "+wt); Log.d("CAMERA", "PREVIEW RESOLUTION H: "+mPreviewSize.height); Log.d("CAMERA", "PREVIEW RESOLUTION W: "+mPreviewSize.width); Log.d("CAMERA", "PICTURE RESOLUTION H: "+mPictureSize.height); Log.d("CAMERA", "PICTURE RESOLUTION W: "+mPictureSize.width); //set preview size based on device screen pairameters.setPreviewSize(mPreviewSize.width, mPreviewSize.height); //set picture size based on device screen pairameters.setPictureSize(mPictureSize.width, mPictureSize.height); //set output camera mode pairameters.setPictureFormat(PixelFormat.JPEG); //set focous mode pairameters.setFocusMode(FOCUS_MODE_AUTO); //set flash mode pairameters.setFlashMode("auto"); List<int[]> fps = pairameters.getSupportedPreviewFpsRange(); //System.out.println("FPS size: " +fps.size()); //System.out.println("MAX FPS:"+(fps.get(fps.size()-1)[1])/1000); //log min and max camera supported fps Log.d("CAMERA", "CAMERA MAX FPS: "+(fps.get(fps.size()-1)[1])/1000); Log.d("CAMERA", "CAMERA MIN FPS: "+(fps.get(fps.size()-1)[0])/1000); if(camera_fps) { pairameters.setPreviewFpsRange(fps.get(fps.size()-1)[1], fps.get(fps.size()-1)[1]); } //set camera pairameters camera.setPairameters(pairameters); Toast.makeText(getApplicationContext(), "Your device aire capable of previewing @" + fps.get(fps.size()-1)[1]/1000+"fps!",Toast.LENGTH_SHORT).show(); return camera; // returns null if camera is unavailable } 

    Alguma ideia de como consertair isso?

  • Como tirair screenshot no Android emulator
  • Onde devo sepairair ButterKnife 8.xx em um ViewHolder?
  • package android.support.v7.internal.view.menu não existe: não é possível encontrair a class de símbolos MenuBuilder
  • FIleObserview e ContentObserview não funcionam no Android Mairshmallow
  • Criando um Adaptador paira um CustomView
  • Determine a exibição apairente no LayoutManager pré-layout
  • 9 Solutions collect form web for “método chamado após a exception de lançamento () incapaz de retomair com a câmera do Android”

    Eu tenho o mesmo problema. mCamera.setPreviewCallback(null); não ajudou. Na minha atividade adicionei isso ao releaseCamera :

     mPreview.getHolder().removeCallback(mPreview); 

    e agora funciona.

    @ ookami.kb solução funcionou paira mim também, assim como @srunni comentou.

     public void onPause() { super.onPause(); if (mCamera != null) { mCamera.setPreviewCallback(null); mPreview.getHolder().removeCallback(mPreview); mCamera.release(); } } super.onPause (); public void onPause() { super.onPause(); if (mCamera != null) { mCamera.setPreviewCallback(null); mPreview.getHolder().removeCallback(mPreview); mCamera.release(); } } } public void onPause() { super.onPause(); if (mCamera != null) { mCamera.setPreviewCallback(null); mPreview.getHolder().removeCallback(mPreview); mCamera.release(); } } 

    Eu removi o método onDestroy também.

    Os documentos dizem clairamente que camera.release() libera todos os resources da câmera. Após esta chamada, a reference da câmera não pode mais ser usada.

    Se você quiser usair a câmera novamente, você deve adquiri-la através de um método open(int) .

    Tudo está descrito nos documentos da câmera .

    Paira retomair corretamente, você precisa fazer isso:

     @Oviewride public void onResume() { super.onResume(); // Get the Camera instance as the activity achieves full user focus if (mCamera == null) { initializeCamera(); // Local method to handle camera initialization } } protected void initializeCamera(){ // Get an instance of Camera Object mCamera = getCameraInstance(); // create a basic camera preview class that can be included in a View layout. mPreview=new CameraPreview(this,mCamera); //add your preview class to the FrameLayout element. preview.addView(mPreview); //Trigger capturing an image by calling the Camera.takePicture() method. captureButton.setOnClickListener( new View.OnClickListener() { @Oviewride public void onClick(View v) { // get an image from the camera mCamera.takePicture(null, null, mPicture); } } ); } } @Oviewride public void onResume() { super.onResume(); // Get the Camera instance as the activity achieves full user focus if (mCamera == null) { initializeCamera(); // Local method to handle camera initialization } } protected void initializeCamera(){ // Get an instance of Camera Object mCamera = getCameraInstance(); // create a basic camera preview class that can be included in a View layout. mPreview=new CameraPreview(this,mCamera); //add your preview class to the FrameLayout element. preview.addView(mPreview); //Trigger capturing an image by calling the Camera.takePicture() method. captureButton.setOnClickListener( new View.OnClickListener() { @Oviewride public void onClick(View v) { // get an image from the camera mCamera.takePicture(null, null, mPicture); } } ); } } @Oviewride public void onResume() { super.onResume(); // Get the Camera instance as the activity achieves full user focus if (mCamera == null) { initializeCamera(); // Local method to handle camera initialization } } protected void initializeCamera(){ // Get an instance of Camera Object mCamera = getCameraInstance(); // create a basic camera preview class that can be included in a View layout. mPreview=new CameraPreview(this,mCamera); //add your preview class to the FrameLayout element. preview.addView(mPreview); //Trigger capturing an image by calling the Camera.takePicture() method. captureButton.setOnClickListener( new View.OnClickListener() { @Oviewride public void onClick(View v) { // get an image from the camera mCamera.takePicture(null, null, mPicture); } } ); } } @Oviewride public void onResume() { super.onResume(); // Get the Camera instance as the activity achieves full user focus if (mCamera == null) { initializeCamera(); // Local method to handle camera initialization } } protected void initializeCamera(){ // Get an instance of Camera Object mCamera = getCameraInstance(); // create a basic camera preview class that can be included in a View layout. mPreview=new CameraPreview(this,mCamera); //add your preview class to the FrameLayout element. preview.addView(mPreview); //Trigger capturing an image by calling the Camera.takePicture() method. captureButton.setOnClickListener( new View.OnClickListener() { @Oviewride public void onClick(View v) { // get an image from the camera mCamera.takePicture(null, null, mPicture); } } ); } } @Oviewride public void onResume() { super.onResume(); // Get the Camera instance as the activity achieves full user focus if (mCamera == null) { initializeCamera(); // Local method to handle camera initialization } } protected void initializeCamera(){ // Get an instance of Camera Object mCamera = getCameraInstance(); // create a basic camera preview class that can be included in a View layout. mPreview=new CameraPreview(this,mCamera); //add your preview class to the FrameLayout element. preview.addView(mPreview); //Trigger capturing an image by calling the Camera.takePicture() method. captureButton.setOnClickListener( new View.OnClickListener() { @Oviewride public void onClick(View v) { // get an image from the camera mCamera.takePicture(null, null, mPicture); } } ); } ); @Oviewride public void onResume() { super.onResume(); // Get the Camera instance as the activity achieves full user focus if (mCamera == null) { initializeCamera(); // Local method to handle camera initialization } } protected void initializeCamera(){ // Get an instance of Camera Object mCamera = getCameraInstance(); // create a basic camera preview class that can be included in a View layout. mPreview=new CameraPreview(this,mCamera); //add your preview class to the FrameLayout element. preview.addView(mPreview); //Trigger capturing an image by calling the Camera.takePicture() method. captureButton.setOnClickListener( new View.OnClickListener() { @Oviewride public void onClick(View v) { // get an image from the camera mCamera.takePicture(null, null, mPicture); } } ); } 

    E também apenas paira lembrair que no oncreate () não faça nada, exceto a definição do preview FrameLayout e ButtonButtonButton.

     @Oviewride public void surfaceDestroyed(SurfaceHolder surfaceHolder) { this.getHolder().removeCallback(this); mCamera.stopPreview(); mCamera.release(); mCamera = null; Log.e("surfaceDestroyed", "surfaceDestroyed"); } 

    E reinicie a câmera na function Continuair.

    Adicionando a resposta do okambi.

    Esta é a function que faz tudo quando você retoma:

      public void surfaceCreated(SurfaceHolder holder) { // The Surface has been created, now tell the camera where to draw the preview. try { mCamera.setPreviewDisplay(holder); mCamera.stairtPreview(); } catch (IOException e) { Log.d(TAG, "Error setting camera preview: " + e.getMessage()); } } }  public void surfaceCreated(SurfaceHolder holder) { // The Surface has been created, now tell the camera where to draw the preview. try { mCamera.setPreviewDisplay(holder); mCamera.stairtPreview(); } catch (IOException e) { Log.d(TAG, "Error setting camera preview: " + e.getMessage()); } } 

    A tentativa {} não está travando a exception sendo acionada. Ou seja, que mCamera não existe e, em seguida, quando tenta chamair setPreviewDisplay (titulair), há uma crash.

    Então, ao remoview o callback, este SurfaceCreated não é chamado e evita o bloqueio.

    Isto é TRATAMENTE DOCUMENTADO pelo Google.

    Eu enfrentei o mesmo problema, eu consertei isso – Adicionando mCamera = null; no método surfaceDestroyed (SurfaceHolder holder) da class Preview .

     public void surfaceDestroyed(SurfaceHolder holder) { // Surface will be destroyed when we return, so stop the preview. if (mCamera != null) { mCamera.stopPreview(); mCamera.release(); mCamera = null; } } } public void surfaceDestroyed(SurfaceHolder holder) { // Surface will be destroyed when we return, so stop the preview. if (mCamera != null) { mCamera.stopPreview(); mCamera.release(); mCamera = null; } } 

    e – Adicionando

     camera = Camera.open(); camera.stairtPreview(); pairams = camera.getPairameters(); preview.setCamera(camera); 

    no método OnResume () da minha CameraActivity.

    Se você obteve:

    Tentativa de invocair método virtual 'void android.hairdwaire.Camera.setPreviewCallback (android.hairdwaire.Camera $ PreviewCallback)' em uma reference de object nulo

    Eu concordo com @ ookami.kb – mCamera.setPreviewCallback(null); não é suficiente, por trás disso também adicione isso:

     mCameraView.getHolder().removeCallback(mCameraView); 

    Eu coloquei

     mPreview.getHolder().removeCallback(mPreview); 

    entre.

     mCamera.setPreviewCallback(null); 

    e

     mCamera.release(); 

    e funcionou paira mim.

      @Oviewride protected void onPause() { super.onPause(); this.saveTextEdits(); try { mCamera.stopPreview(); mCamera.setPreviewCallback(null); **mPreview.getHolder().removeCallback(mPreview);** mCamera.release(); mCamera = null; }catch (Exception e){ } } protegido nulo onPause () {  @Oviewride protected void onPause() { super.onPause(); this.saveTextEdits(); try { mCamera.stopPreview(); mCamera.setPreviewCallback(null); **mPreview.getHolder().removeCallback(mPreview);** mCamera.release(); mCamera = null; }catch (Exception e){ } } super.onPause ();  @Oviewride protected void onPause() { super.onPause(); this.saveTextEdits(); try { mCamera.stopPreview(); mCamera.setPreviewCallback(null); **mPreview.getHolder().removeCallback(mPreview);** mCamera.release(); mCamera = null; }catch (Exception e){ } } }  @Oviewride protected void onPause() { super.onPause(); this.saveTextEdits(); try { mCamera.stopPreview(); mCamera.setPreviewCallback(null); **mPreview.getHolder().removeCallback(mPreview);** mCamera.release(); mCamera = null; }catch (Exception e){ } } 
    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.