Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6603619
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:02:21+00:00 2026-05-25T19:02:21+00:00

I am getting an exception using a custom front camera. On button click I

  • 0

I am getting an exception using a custom front camera. On button click I am calling camera.takepicture. Can anybody tell me what the problem is and how to fix it?

 09-23 11:24:35.062: ERROR/AndroidRuntime(949): FATAL EXCEPTION: main
09-23 11:24:35.062: ERROR/AndroidRuntime(949): java.lang.RuntimeException: Method called after release()
09-23 11:24:35.062: ERROR/AndroidRuntime(949):     at android.hardware.Camera.native_takePicture(Native Method)
09-23 11:24:35.062: ERROR/AndroidRuntime(949):     at android.hardware.Camera.takePicture(Camera.java:746)
09-23 11:24:35.062: ERROR/AndroidRuntime(949):     at com.camera.test.CameraActivity.onClick(CameraActivity.java:42)
09-23 11:24:35.062: ERROR/AndroidRuntime(949):     at android.view.View.performClick(View.java:2485)
09-23 11:24:35.062: ERROR/AndroidRuntime(949):     at android.view.View$PerformClick.run(View.java:9080)
09-23 11:24:35.062: ERROR/AndroidRuntime(949):     at android.os.Handler.handleCallback(Handler.java:587)
09-23 11:24:35.062: ERROR/AndroidRuntime(949):     at android.os.Handler.dispatchMessage(Handler.java:92)
09-23 11:24:35.062: ERROR/AndroidRuntime(949):     at android.os.Looper.loop(Looper.java:123)
09-23 11:24:35.062: ERROR/AndroidRuntime(949):     at android.app.ActivityThread.main(ActivityThread.java:3683)
09-23 11:24:35.062: ERROR/AndroidRuntime(949):     at java.lang.reflect.Method.invokeNative(Native Method)
09-23 11:24:35.062: ERROR/AndroidRuntime(949):     at java.lang.reflect.Method.invoke(Method.java:507)
09-23 11:24:35.062: ERROR/AndroidRuntime(949):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
09-23 11:24:35.062: ERROR/AndroidRuntime(949):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
09-23 11:24:35.062: ERROR/AndroidRuntime(949):     at dalvik.system.NativeStart.main(Native Method)
09-23 11:24:35.082: WARN/ActivityManager(107):   Force finishing activity com.camera.test/.CameraActivity

my xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:configChanges="orientation">
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <Button android:layout_height="wrap_content" android:layout_width="match_parent" android:text="Button" android:id="@+id/button1">
        </Button>
        <com.camera.test.Preview
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/preview" android:layout_marginTop="100px"/>   
    </LinearLayout>
</FrameLayout>

my code

import java.io.DataOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.NoSuchElementException;
import android.app.Activity;
import android.content.Context;
import android.hardware.Camera;
import android.os.Bundle;
import android.os.Environment;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class CameraActivity extends Activity implements OnClickListener {
    Preview p=null;
    private Button button1=null;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Hide the window title.
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main);
        p=(Preview)findViewById(R.id.preview);
        button1 = (Button) findViewById(R.id.button1);   
        button1.setOnClickListener(this);
        //p.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) 
    {
        p.mCamera.takePicture(shutterCallback, null, jpegCallback);  

  /*    p.mCamera.(new Camera.AutoFocusCallback() 
        {  
            Camera.ShutterCallback shutterCallback = new Camera.ShutterCallback()
            {  
                public void onShutter() 
                {       // Play your sound here.    
                    }  
                };  
                public void onAutoFocus(boolean success, Camera camera)
                {     
                    p.mCamera.takePicture(shutterCallback, null, jpegCallback); 
                    } 
                });*/
                }


        /*p.mCamera.startPreview();
        p.mCamera.takePicture(shutterCallback, null, jpegCallback); 
*/
        // TODO Auto-generated method stub
        /*p.mCamera.autoFocus(new Camera.AutoFocusCallback() 
        {  
            Camera.ShutterCallback shutterCallback = new Camera.ShutterCallback() 
            {     public void onShutter()
            {       
                // Play your sound here.    
            }   
            };  
            public void onAutoFocus(boolean success, Camera camera)
            {    



            }
            });  */ 


     Camera.ShutterCallback shutterCallback = new Camera.ShutterCallback() { // <6>
            public void onShutter() {
                //Log.d(TAG, "onShutter'd");
            }
        };

        //Handles data for raw picture
        Camera.PictureCallback rawCallback = new Camera.PictureCallback() { // <7>
            public void onPictureTaken(byte[] data, Camera camera) {
               // Log.d(TAG, "onPictureTaken - raw");
            }
        };


        // Handles data for jpeg picture
        Camera.PictureCallback jpegCallback = new Camera.PictureCallback() { // <8>
            public void onPictureTaken(byte[] data, Camera camera) {

                 //YuvImage image = new YuvImage(data, parameters.getPreviewFormat(),  size.width, size.height, null); 

                //decodeYUV(argb8888, data, camSize.width, camSize.height);
                //Bitmap bitmap = Bitmap.createBitmap(argb8888, camSize.width,                     
                        //camSize.height, Config.ARGB_8888); 
              // p.mCamera.startPreview();
                Toast.makeText(CameraActivity.this, "Writing a  file", Toast.LENGTH_LONG).show();
                    File sdCard = Environment.getExternalStorageDirectory();

                File dir = new File(sdCard.getAbsolutePath()+"/Vijay");


                  dir.mkdir();


                Toast.makeText(CameraActivity.this, dir.getAbsolutePath(), Toast.LENGTH_LONG).show();
                //dir.mkdirs();
                //Toast.makeText(CameraActivity.this,"%d.jpg"System.currentTimeMillis()+"", Toast.LENGTH_LONG).show();

                File out = new File(dir,String.format("%d.jpg", System.currentTimeMillis()));
                try {


                    out.createNewFile();
                    Toast.makeText(CameraActivity.this, "created new file", Toast.LENGTH_LONG).show();

                    Toast.makeText(CameraActivity.this, out.getAbsolutePath(), Toast.LENGTH_LONG).show();

                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
                DataOutputStream fo = null;

                    try {
                        fo = new DataOutputStream( new FileOutputStream(out));
                        //write what you want to fo
                        fo.write(data);
                        fo.close();
                    } 
                    catch (Exception e) {
                        // TODO Auto-generated catch block

                         Toast.makeText(CameraActivity.this, "entering exception",Toast.LENGTH_LONG).show();
                        PrintWriter pw;
                        try
                        {
                            File sdCard1 = Environment.getExternalStorageDirectory();
                            File dir1 = new File(sdCard1.getAbsolutePath()+"/Log");


                               dir1.mkdir();

                           File out1 = new File(dir1,String.format("%d.txt", System.currentTimeMillis()));



                                try {
                                    out1.createNewFile();
                                } catch (IOException e1) {
                                    // TODO Auto-generated catch block
                                    e1.printStackTrace();
                                }

                            pw = new PrintWriter(out1);
                            e.printStackTrace(pw); 

                        } 
                        catch (FileNotFoundException e1) {
                            // TODO Auto-generated catch block
                            e1.printStackTrace();
                           //

                        }      
                            // TODO: handle exception

                        //e.printStackTrace();

                    }
                    Toast.makeText(getBaseContext(), "Preview", Toast.LENGTH_SHORT).show();
                    //camera.release();



            }
        };


}

this is my preview

 package com.camera.test;
import java.io.IOException;
import java.util.NoSuchElementException;

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

class Preview extends SurfaceView implements SurfaceHolder.Callback {

     Camera mCamera;
    private final SurfaceHolder mHolder;
    final static int SUPPORTED_WIDTH = 640;
    final static int SUPPORTED_HEIGHT = 480;

    public Preview(Context context, AttributeSet attributes) {
        super(context, attributes);
        // Install a SurfaceHolder.Callback so we get notified when the
        // underlying surface is created and destroyed.
        mHolder = getHolder();
        mHolder.addCallback(this);
        mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }

    /**
     * Works in API level >= 10.
     *
     * @return Front camera handle.
     */
    Camera getFrontFacingCamera() throws NoSuchElementException 
    {
        final Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
        for (int cameraIndex = 0; cameraIndex < Camera.getNumberOfCameras(); cameraIndex++) 
        {
            Camera.getCameraInfo(cameraIndex, cameraInfo);
            if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT)
            {
                try {
                    return Camera.open(cameraIndex);
                } catch (final RuntimeException e)
                {
                    e.printStackTrace();
                }
            }
        }
        throw new NoSuchElementException("Can't find front camera.");
    }

    /**
     * Works in API level >= 7 at Samsung Galaxy S.
     *
     * @param Camera handle.
     */
    void setFrontCamera(Camera camera) {
        final Camera.Parameters parameters = camera.getParameters();
        parameters.set("camera-id", 2);
        try {
            camera.setParameters(parameters);
        } catch (final RuntimeException e) {
            // If we can't set front camera it means that device hasn't got "camera-id". Maybe it's not Galaxy S.
            e.printStackTrace();
        }
    }

    /**
     * @see android.view.SurfaceHolder.Callback#surfaceChanged(android.view.SurfaceHolder, int, int, int)
     */
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        // Now that the size is known, set up the camera parameters and begin the preview.
        final Camera.Parameters parameters = mCamera.getParameters();
        parameters.setPreviewSize(SUPPORTED_WIDTH, SUPPORTED_HEIGHT);
        mCamera.setParameters(parameters);
        mCamera.startPreview();
    }

    /**
     * The Surface has been created, acquire the camera and tell it where to draw.
     * 
     * @see android.view.SurfaceHolder.Callback#surfaceCreated(android.view.SurfaceHolder)
     */
    public void surfaceCreated(SurfaceHolder holder) {
        if (android.os.Build.VERSION.SDK_INT >= 10) {
            mCamera = getFrontFacingCamera();
            mCamera.setDisplayOrientation(90);
        } else {
            mCamera = Camera.open();
            setFrontCamera(mCamera);
        }
        try {
            mCamera.setPreviewDisplay(holder);
        } catch (final IOException e) {
            //mCamera.setPreviewCallback(null); 
            mCamera.release();
            mCamera = null;
            e.printStackTrace();
        }
    }

    /**
     * @see android.view.SurfaceHolder.Callback#surfaceDestroyed(android.view.SurfaceHolder)
     */
    public void surfaceDestroyed(SurfaceHolder holder) {
        // Surface will be destroyed when we return, so stop the preview.
        // Because the CameraDevice object is not a shared resource, it's very
        // important to release it when the activity is paused.
        mCamera.stopPreview();
        //mCamera.setPreviewCallback(null); 
        mCamera.release();
        mCamera = null;
    }

}

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T19:02:22+00:00Added an answer on May 25, 2026 at 7:02 pm

    Uncomment the line mCamera.setPreviewCallback(null); in public void surfaceDestroyed(SurfaceHolder holder) callback.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm getting an exception calling Resolve: KernelException: Could not instantiate custom activator Inner Exception:
I'm trying to serialize a type using the DataContractSerializer and am getting the exception
I'm new to using Hibernate with Java. I'm getting the following exception. The stuff
I'm getting NullPointerException :s when using sstable2json in Cassandra 0.6.0-beta3: $ bin/sstable2json .../cassandra/data/system/LocationInfo-1-Data.db Exception
What is the best practice way of getting Exception Transparency in Java when using
I'm getting an exception when using html helpers in mvc3. I got the exception
i am using google custom search engine and getting the results in JSON format.for
I have a custom exception filter that I'm using to catch a custom exception
I'm using the Post method so I can send a custom object. But I
I'm gettting the following exception when performing an insert to an Oracle Databse using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.