Android, probleme on camera preview: error starting camera preview: startpreview failed, camera server died, icamera dier, error 100
<p>i can't see the camea preview. i got some errors in logcat and the camera preview doesn't start. this is the logcat:</p>

<pre><code>01-26 13:56:14.536: W/Trace(972): Unexpected value from nativeGetEnabledTags: 0
01-26 13:56:23.626: W/Trace(1086): Unexpected value from nativeGetEnabledTags: 0
01-26 13:56:23.626: W/Trace(1086): Unexpected value from nativeGetEnabledTags: 0
01-26 13:56:23.636: W/Trace(1086): Unexpected value from nativeGetEnabledTags: 0
01-26 13:56:24.036: W/Trace(1086): Unexpected value from nativeGetEnabledTags: 0
01-26 13:56:24.036: W/Trace(1086): Unexpected value from nativeGetEnabledTags: 0
01-26 13:56:24.296: I/System.out(1086): step1
01-26 13:56:24.306: I/System.out(1086): check
01-26 13:56:24.928: I/System.out(1086): camget=android.hardware.Camera@40ce6800
01-26 13:56:24.928: I/System.out(1086): step2
01-26 13:56:24.936: I/System.out(1086): cam=android.hardware.Camera@40ce6800
01-26 13:56:24.936: I/System.out(1086): step2.1
01-26 13:56:25.176: I/System.out(1086): step3
01-26 13:56:25.176: I/System.out(1086): step4
01-26 13:56:25.236: I/System.out(1086): step5
01-26 13:56:25.236: I/System.out(1086): step6
01-26 13:56:25.236: I/System.out(1086): step7
01-26 13:56:25.316: W/Trace(1086): Unexpected value from nativeGetEnabledTags: 0
01-26 13:56:25.326: W/Trace(1086): Unexpected value from nativeGetEnabledTags: 0
01-26 13:56:25.336: W/Trace(1086): Unexpected value from nativeGetEnabledTags: 0
01-26 13:56:25.366: W/Trace(1086): Unexpected value from nativeGetEnabledTags: 0
01-26 13:56:25.467: D/libEGL(1086): loaded /system/lib/egl/libEGL_emulation.so
01-26 13:56:25.628: D/(1086): HostConnection::get() New Host Connection established 0x2a154918, tid 1086
01-26 13:56:25.706: D/libEGL(1086): loaded /system/lib/egl/libGLESv1_CM_emulation.so
01-26 13:56:25.716: D/libEGL(1086): loaded /system/lib/egl/libGLESv2_emulation.so
01-26 13:56:25.776: W/EGL_emulation(1086): eglSurfaceAttrib not implemented
01-26 13:56:25.787: D/OpenGLRenderer(1086): Enabling debug mode 0
01-26 13:56:25.787: W/Trace(1086): Unexpected value from nativeGetEnabledTags: 0
01-26 13:56:25.806: W/Trace(1086): Unexpected value from nativeGetEnabledTags: 0
01-26 13:56:26.006: D/(1086): Error starting camera preview: startPreview failed
01-26 13:56:26.047: W/Trace(1086): Unexpected value from nativeGetEnabledTags: 0
01-26 13:56:26.056: I/Choreographer(1086): Skipped 41 frames!  The application may be doing too much work on its main thread.
01-26 13:56:26.066: W/Trace(1086): Unexpected value from nativeGetEnabledTags: 0
01-26 13:56:26.216: W/Trace(1086): Unexpected value from nativeGetEnabledTags: 0
01-26 13:56:26.216: W/Trace(1086): Unexpected value from nativeGetEnabledTags: 0
01-26 13:56:26.276: W/Trace(1086): Unexpected value from nativeGetEnabledTags: 0
01-26 13:56:26.296: W/Trace(1086): Unexpected value from nativeGetEnabledTags: 0
01-26 13:56:26.296: W/Trace(1086): Unexpected value from nativeGetEnabledTags: 0
01-26 13:56:26.306: W/Trace(1086): Unexpected value from nativeGetEnabledTags: 0
01-26 13:56:26.596: W/Trace(1086): Unexpected value from nativeGetEnabledTags: 0
01-26 13:56:26.697: W/Trace(1086): Unexpected value from nativeGetEnabledTags: 0
01-26 13:56:26.766: W/Trace(1086): Unexpected value from nativeGetEnabledTags: 0
01-26 13:56:27.596: W/Trace(1086): Unexpected value from nativeGetEnabledTags: 0
01-26 13:56:27.697: W/Trace(1086): Unexpected value from nativeGetEnabledTags: 0
01-26 13:56:27.846: W/Trace(1086): Unexpected value from nativeGetEnabledTags: 0
01-26 13:56:27.846: W/Trace(1086): Unexpected value from nativeGetEnabledTags: 0
01-26 13:56:27.846: W/Trace(1086): Unexpected value from nativeGetEnabledTags: 0
01-26 13:56:27.876: W/Trace(1086): Unexpected value from nativeGetEnabledTags: 0
01-26 13:56:27.876: W/Trace(1086): Unexpected value from nativeGetEnabledTags: 0
01-26 13:56:27.906: W/Trace(1086): Unexpected value from nativeGetEnabledTags: 0
01-26 13:56:27.906: W/Trace(1086): Unexpected value from nativeGetEnabledTags: 0
01-26 13:56:27.906: W/Trace(1086): Unexpected value from nativeGetEnabledTags: 0
01-26 13:56:27.916: W/Trace(1086): Unexpected value from nativeGetEnabledTags: 0
01-26 13:56:27.926: W/Trace(1086): Unexpected value from nativeGetEnabledTags: 0
01-26 13:56:28.046: W/Camera(1086): Camera server died!
01-26 13:56:28.056: W/Camera(1086): ICamera died
01-26 13:56:28.106: W/Trace(1086): Unexpected value from nativeGetEnabledTags: 0
01-26 13:56:28.116: W/Trace(1086): Unexpected value from nativeGetEnabledTags: 0
01-26 13:56:28.126: E/Camera(1086): Error 100
</code></pre>

<p>a message says: Skipped 41 frames!  The application may be doing too much work on its main thread. i use only one threa, is this a problem?</p>

<p>the complete code is this: </p>

<pre><code>        import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

import java.io.IOException;

import android.content.Context;
import android.content.pm.PackageManager;
import android.view.SurfaceView;
import android.view.SurfaceHolder;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;


public class MainActivity extends Activity
{

    private Camera mCamera;
    private CameraPreview mPreview;

    private class HandlePictureStorage implements PictureCallback
    {
        @Override
        public void onPictureTaken(byte[] picture, Camera camera) 
        {
        }
}    

    public static Camera getCameraInstance()
    {
        Camera c = null;

        try 
        {
            c = Camera.open(); // attempt to get a Camera instance
            System.out.println("camget=" + c);
        }
        catch (Exception e)
        {
            // Camera is not available (in use or does not exist)
            System.out.println("error: " + e);
        }

            return c; 
    }

    /** Check if this device has a camera */
    private Camera checkCameraHardware(Context context) {
        Camera tmp;
        if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)){
            // this device has a camera
            System.out.println("check");
            tmp=getCameraInstance();            
        } else {
            // no camera on this device
            tmp=null;
        }
        return tmp;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        mCamera = checkCameraHardware(this);
        super.onCreate(savedInstanceState);
        if(mCamera!=null)
        {           
        setContentView(R.layout.activity_main);


                mPreview = new CameraPreview(this, mCamera);
        LayoutParams camparam = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

        LinearLayout bckgr = (LinearLayout) findViewById(R.id.bkgrnd);        

        try
                {

            bckgr.addView(mPreview,camparam);

                }
                catch(NullPointerException e) 
                {
                   System.out.println("L'ERRORE E'" + e + "," + bckgr + "," + mCamera);
                 }
                catch(RuntimeException e)
        {
            System.out.println("L'ERRORE E'" + e + "," + bckgr + "," + mCamera);
        }
        catch(Exception e)
        {
            System.out.println("L'ERRORE E'" + e + "," + bckgr + "," + mCamera);
        }
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

}



import java.io.IOException;
import java.util.Iterator;
import java.util.List;

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

/** A basic Camera preview class */
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
    private static final String TAG = null;
    private SurfaceHolder mHolder;
    private Camera mCamera;

    public CameraPreview(Context context, Camera camera) {
        super(context);
        mCamera = camera;

        // Install a SurfaceHolder.Callback so we get notified when the
        // underlying surface is created and destroyed.
        mHolder = getHolder();
        mHolder.addCallback(this);
        // deprecated setting, but required on Android versions prior to 3.0
        mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        // The Surface has been created, now tell the camera where to draw the preview.
        try {
            mCamera.setPreviewDisplay(holder);
            //mCamera.setDisplayOrientation(90);
            Camera.Parameters parameters = mCamera.getParameters();


            mCamera.startPreview();
        } catch (IOException e) {
            Log.d(TAG, "Error setting camera preview: " + e.getMessage());
        }
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
        // empty. Take care of releasing the Camera preview in your activity.
        mCamera.stopPreview();
        mCamera.release();
        mCamera = null;

    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
        // If your preview can change or rotate, take care of those events here.
        // Make sure to stop the preview before resizing or reformatting it.

        if (mHolder.getSurface() == null){
          // preview surface does not exist
          return;
        }

        // stop preview before making changes
        try {
            mCamera.stopPreview();
        } catch (Exception e){
          // ignore: tried to stop a non-existent preview
        }

        // set preview size and make any resize, rotate or
        // reformatting changes here

        // start preview with new settings
        try {
            mCamera.setPreviewDisplay(mHolder);
            mCamera.startPreview();

        } catch (Exception e){
            Log.d(TAG, "Error starting camera preview: " + e.getMessage());
        }

        // Now that the size is known, set up the camera parameters and begin
        // the preview.

    }
}
</code></pre>

<p>i used both Camera.open() and Camera.opent(id).  </p>
