Search code examples
androidandroid-camerasurfaceviewsurfaceholder

switching a camera from back to front shows java.lang.runtimeexception method called after release()


I am developing my own Android camera app.what I would like to do is when I open my app it has to show two buttons likely capture and take photo along with camera screen.when I click the capture button I can take image of current position(from back camera).after capturing this photo if I click the take photo button I would like to capture photo from front camera but it shows java.lang.runtimeexception method called after release().Here I attached my code.So any one please help me to resolve the error.

MainActivity.java

package com.example.camera1;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends Activity {

       private Camera cameraObject;
       private ShowCamera showCamera;
       private ImageView pic;
       private Button takePhotoButton;
       FrameLayout preview;
       public static Camera isCameraAvailiable(){
          Camera object = null;
          try {

             object = Camera.open(); 
          }
          catch (Exception e){
          }
          return object; 
       }

       private PictureCallback capturedIt = new PictureCallback() {

          @Override
          public void onPictureTaken(byte[] data, Camera camera) {

          Bitmap bitmap = BitmapFactory.decodeByteArray(data , 0, data .length);
          if(bitmap==null){
             Toast.makeText(getApplicationContext(), "not taken", Toast.LENGTH_SHORT).show();
          }
          else
          {
             Toast.makeText(getApplicationContext(), "taken", Toast.LENGTH_SHORT).show();       
          }

       }
    };

       @Override
       protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);

          takePhotoButton = (Button)findViewById(R.id.button_takephoto);
          cameraObject = isCameraAvailiable();
          showCamera = new ShowCamera(this, cameraObject);
          preview = (FrameLayout) findViewById(R.id.camera_preview);
          preview.addView(showCamera);



          takePhotoButton.setOnClickListener(new OnClickListener() {

            @SuppressWarnings("static-access")
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                  cameraObject.stopPreview();
                             preview.removeView(showCamera);
                cameraObject.release();
                cameraObject.open(1);


                    showCamera = new ShowCamera(MainActivity.this, cameraObject);

                      preview.addView(showCamera);
            }
        });

       }
       public void snapIt(View view){
          cameraObject.takePicture(null, null, capturedIt);
       }

       @Override
       public void onPause(){
           super.onPause();
           if (cameraObject != null) {
               cameraObject.stopPreview();
               cameraObject.release();
               cameraObject = null;
            }

              preview.removeView(showCamera);
       }

       @Override
       public boolean onCreateOptionsMenu(Menu menu) {
          getMenuInflater().inflate(R.menu.activity_main, menu);
          return true;
       }
    }

ShowCamera.java

package com.example.camera1;

import java.io.IOException;

import android.content.Context;
import android.hardware.Camera;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class ShowCamera extends SurfaceView implements SurfaceHolder.Callback {

       private SurfaceHolder holdMe;
       private Camera theCamera;

       public ShowCamera(Context context,Camera camera) {
          super(context);
          theCamera = camera;
          holdMe = getHolder();
          holdMe.addCallback(this);
       }

       @Override
       public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
       }

       @Override
       public void surfaceCreated(SurfaceHolder holder) {
          try   {
              theCamera.setDisplayOrientation(90);
             theCamera.setPreviewDisplay(holder);
             theCamera.startPreview(); 
          } catch (IOException e) {
          }
       }

       @Override
       public void surfaceDestroyed(SurfaceHolder arg0) {
           this.getHolder().removeCallback(this);
           theCamera.stopPreview(); 
           theCamera.release();
       }

    }

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:orientation="horizontal" >

   <LinearLayout
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:layout_weight="0.30"
      android:orientation="vertical" >

         <FrameLayout
            android:id="@+id/camera_preview"
            android:layout_width="fill_parent"
            android:layout_height="350dp" />

         <RelativeLayout
             android:layout_width="match_parent"
             android:layout_height="wrap_content" >

             <Button
                 android:id="@+id/button_capture"
                 android:layout_width="match_parent"
                 android:layout_height="wrap_content"
                 android:onClick="snapIt"
                 android:text="@string/Capture" />

             <Button
                 android:id="@+id/button_takephoto"
                 android:layout_width="match_parent"
                 android:layout_height="wrap_content"
                 android:layout_below="@+id/button_capture"
                 android:text="Take Photo" />
         </RelativeLayout>

   </LinearLayout>

</LinearLayout>

LogCat output

05-29 12:42:56.378: E/AndroidRuntime(15990): FATAL EXCEPTION: main
05-29 12:42:56.378: E/AndroidRuntime(15990): java.lang.RuntimeException: Method called after release()
05-29 12:42:56.378: E/AndroidRuntime(15990):    at android.hardware.Camera.setDisplayOrientation(Native Method)
05-29 12:42:56.378: E/AndroidRuntime(15990):    at com.example.camera1.ShowCamera.surfaceCreated(ShowCamera.java:29)
05-29 12:42:56.378: E/AndroidRuntime(15990):    at android.view.SurfaceView.updateWindow(SurfaceView.java:580)
05-29 12:42:56.378: E/AndroidRuntime(15990):    at android.view.SurfaceView.access$000(SurfaceView.java:83)
05-29 12:42:56.378: E/AndroidRuntime(15990):    at android.view.SurfaceView$3.onPreDraw(SurfaceView.java:177)
05-29 12:42:56.378: E/AndroidRuntime(15990):    at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:726)
05-29 12:42:56.378: E/AndroidRuntime(15990):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2084)
05-29 12:42:56.378: E/AndroidRuntime(15990):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1125)
05-29 12:42:56.378: E/AndroidRuntime(15990):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4607)
05-29 12:42:56.378: E/AndroidRuntime(15990):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:747)
05-29 12:42:56.378: E/AndroidRuntime(15990):    at android.view.Choreographer.doCallbacks(Choreographer.java:567)
05-29 12:42:56.378: E/AndroidRuntime(15990):    at android.view.Choreographer.doFrame(Choreographer.java:536)
05-29 12:42:56.378: E/AndroidRuntime(15990):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:733)
05-29 12:42:56.378: E/AndroidRuntime(15990):    at android.os.Handler.handleCallback(Handler.java:615)
05-29 12:42:56.378: E/AndroidRuntime(15990):    at android.os.Handler.dispatchMessage(Handler.java:92)
05-29 12:42:56.378: E/AndroidRuntime(15990):    at android.os.Looper.loop(Looper.java:153)
05-29 12:42:56.378: E/AndroidRuntime(15990):    at android.app.ActivityThread.main(ActivityThread.java:5086)
05-29 12:42:56.378: E/AndroidRuntime(15990):    at java.lang.reflect.Method.invokeNative(Native Method)
05-29 12:42:56.378: E/AndroidRuntime(15990):    at java.lang.reflect.Method.invoke(Method.java:511)
05-29 12:42:56.378: E/AndroidRuntime(15990):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
05-29 12:42:56.378: E/AndroidRuntime(15990):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
05-29 12:42:56.378: E/AndroidRuntime(15990):    at dalvik.system.NativeStart.main(Native Method)

Solution

  • Change cameraObject.open(1);

    to

    cameraObject = Camera.open(1);

    inside method call onclick() for button takePhotoButton. It will work.