안녕하세요

카메라 프리뷰를 띄우는 코드를 보고 작성을해서 실행을 해 보았는데
처음 어플을 실행하고 종료한후에 다시 어플을 실행하면 에러메시지가 뜹니다.

하지만 에러 메시지를 닫고 다시 어플을 실행하면 어플이 정상 동작하는 현상이 생깁니다.
(즉, 어플실행 -> 종료 -> 어플실행 -> 에러 -> 에러창 닫고 다시 어플실행 -> 정상동작  이 반복이 됩니다.)

어느부분이 문제가 있는지 잘 모르겠습니다.
1.6버전으로 테스트 했습니다.

 code아래와 같습니다.
 package android.demo;

import java.io.IOException;

import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.hardware.Camera;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.Window;

public class camera16 extends Activity {    
    private Preview mPreview;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        // Hide the window title.
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        
        // Create our Preview view and set it as the content of our activity.
        mPreview = new Preview(this);
        setContentView(mPreview);
    }   
}

// ----------------------------------------------------------------------

class Preview extends SurfaceView implements SurfaceHolder.Callback {
    SurfaceHolder mHolder;
    Camera mCamera;
    
    Preview(Context context) {
        super(context);
        
        // 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);
    }

    public void surfaceCreated(SurfaceHolder holder) {
        // The Surface has been created, acquire the camera and tell it where
        // to draw.
        mCamera = Camera.open();
        try {
           mCamera.setPreviewDisplay(holder);
        } catch (IOException exception) {
            mCamera.release();
            mCamera = null;
            // TODO: add more exception handling logic here
        }
    }

    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 = null;
    }

    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
        // Now that the size is known, set up the camera parameters and begin
        // the preview.
        Camera.Parameters parameters = mCamera.getParameters();
        parameters.setPreviewSize(w, h);
        mCamera.setParameters(parameters);
        mCamera.startPreview();
    }
}



처음실행후 종료하고
다시 실행 했을때의 화면
cameraError.jpeg


처음 실행하고 종료했을때의 log
D/dalvikvm(  101): GC freed 3962 objects / 215304 bytes in 201ms
W/KeyCharacterMap(  301): No keyboard for id 0
W/KeyCharacterMap(  301): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
D/CameraService(  227): stopPreview (pid 301)
D/CameraService(  227): stopPreview(), hardware stopped OK


에러 발생시 log
I/ActivityManager(   53): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=android.demo/.camera16 }
D/CameraService(  227): CameraService::connect E (pid 301, client 0xd8c8)
D/CameraService(  227): CameraService::connect X (pid 301, new client 0xd8c8) rejected. (old pid 301, old client 0xd108)
D/AndroidRuntime(  301): Shutting down VM
W/dalvikvm(  301): threadid=3: thread exiting with uncaught exception (group=0x4001aa28)
E/AndroidRuntime(  301): Uncaught handler: thread main exiting due to uncaught exception
E/AndroidRuntime(  301): java.lang.RuntimeException: Fail to connect to camera service
E/AndroidRuntime(  301):     at android.hardware.Camera.native_setup(Native Method)
E/AndroidRuntime(  301):     at android.hardware.Camera.<init>(Camera.java:102)
E/AndroidRuntime(  301):     at android.hardware.Camera.open(Camera.java:84)
E/AndroidRuntime(  301):     at android.demo.Preview.surfaceCreated(camera16.java:100)
E/AndroidRuntime(  301):     at android.view.SurfaceView.updateWindow(SurfaceView.java:392)
E/AndroidRuntime(  301):     at android.view.SurfaceView.dispatchDraw(SurfaceView.java:264)
E/AndroidRuntime(  301):     at android.view.ViewGroup.drawChild(ViewGroup.java:1524)
E/AndroidRuntime(  301):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1256)
E/AndroidRuntime(  301):     at android.view.View.draw(View.java:6277)
E/AndroidRuntime(  301):     at android.widget.FrameLayout.draw(FrameLayout.java:352)
E/AndroidRuntime(  301):     at android.view.ViewGroup.drawChild(ViewGroup.java:1526)
E/AndroidRuntime(  301):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1256)
E/AndroidRuntime(  301):     at android.view.View.draw(View.java:6277)
E/AndroidRuntime(  301):     at android.widget.FrameLayout.draw(FrameLayout.java:352)
E/AndroidRuntime(  301):     at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1883)
E/AndroidRuntime(  301):     at android.view.ViewRoot.draw(ViewRoot.java:1332)
E/AndroidRuntime(  301):     at android.view.ViewRoot.performTraversals(ViewRoot.java:1097)
E/AndroidRuntime(  301):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1613)
E/AndroidRuntime(  301):     at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(  301):     at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime(  301):     at android.app.ActivityThread.main(ActivityThread.java:4203)
E/AndroidRuntime(  301):     at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(  301):     at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime(  301):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
E/AndroidRuntime(  301):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
E/AndroidRuntime(  301):     at dalvik.system.NativeStart.main(Native Method)
I/Process (   53): Sending signal. PID: 301 SIG: 3
I/dalvikvm(  301): threadid=7: reacting to signal 3
E/dalvikvm(  301): Unable to open stack trace file '/data/anr/traces.txt': Permission denied
W/ActivityManager(   53): Launch timeout has expired, giving up wake lock!
W/ActivityManager(   53): Activity idle timeout for HistoryRecord{43977a38 android.demo/.camera16}

I/Process (  301): Sending signal. PID: 301 SIG: 9
D/CameraService(  227): Client::~Client E (pid 227, client 0xd108)
I/ActivityManager(   53): Process android.demo (pid 301) has died.
I/WindowManager(   53): WIN DEATH: Window{4393f310 android.demo/android.demo.camera16 paused=false}
I/WindowManager(   53): WIN DEATH: Window{43944020 SurfaceView paused=false}
W/UsageStats(   53): Unexpected resume of com.android.launcher while already resumed in android.demo
D/CameraService(  227): Client::disconnect() E (pid 227 client 0xd108)
D/CameraService(  227): hardware teardown
D/CameraService(  227): removeClient (pid 227): no more strong reference
D/CameraService(  227): Client::disconnect() X (pid 227)
D/CameraService(  227): Client::~Client X (pid 227)
W/InputManagerService(   53): Got RemoteException sending setActive(false) notification to pid 301 uid 10026