2012-05-30 53 views
1

我在我的應用程序中使用相機功能。一切工作正常,但當設備被鎖定/睡眠時,返回到應用程序相機部分(SurfaceView)只是黑色。以下是我的代碼。有人可以找出問題嗎?SurfaceView在黑色前景時,爲相機應用程序

java.lang.RuntimeException: Method called after release() 

在這條線:

public class Activity_Camera extends Activity implements SurfaceHolder.Callback, Camera.AutoFocusCallback, Observer 
{ 
    // Surface vars 
    private SurfaceView preview; 
    private SurfaceHolder previewHolder; 

    // Camera vars 
    private Camera mCamera; 
    private boolean mPreviewRunning = false; 
    private boolean mCaptureFrame = false; 
    private int frame_number = 1; 
    private byte[] frame = new byte[1]; 

    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     preview = (SurfaceView) findViewById(R.id.preview); 
     previewHolder = preview.getHolder(); 
     previewHolder.addCallback(this); 
     previewHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 
    } 

    @Override 
    public void onPause() 
    { 
     super.onPause(); 
     if (mPreviewRunning) 
       { 
        mCamera.stopPreview(); 
        mPreviewRunning = false; 
       } 
       mCamera.setPreviewCallback(null); 
       mCamera.release(); 
    } 



    // implements SurfaceHolder.Callback 
    public void surfaceCreated(SurfaceHolder holder) 
    { 
     mCamera = Camera.open(); 
    } 

    // implements SurfaceHolder.Callback 
    public void surfaceDestroyed(SurfaceHolder holder) 
    { 
     mPreviewRunning = false; 
    } 

    // implements Camera.AutoFocusCallback 
    public void onAutoFocus(boolean success, Camera camera) 
    { 

    } 

    PreviewCallback previewCallback = new PreviewCallback() 
    { 
     public void onPreviewFrame(byte[] data, Camera camera) 
     { 

     } 
    }; 

    // implements SurfaceHolder.Callback 
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) 
    { 
     if (mPreviewRunning) 
     { 
      mCamera.stopPreview(); 
     } 

     Camera.Parameters p = mCamera.getParameters(); 
     p.setPreviewSize(640, 480); 
     mCamera.setParameters(p); 

     try 
     { 
      mCamera.setPreviewDisplay(holder); 
     } catch (IOException e) 
     { 
      e.printStackTrace(); 
     } 

     mCamera.setPreviewCallback(previewCallback); 

     int rotation = getWindowManager().getDefaultDisplay().getRotation(); 
     int degrees = 0; 
     switch (rotation) 
     { 
     case Surface.ROTATION_0: 
      degrees = 90; 
      break; 
     case Surface.ROTATION_90: 
      degrees = 0; 
      break; 
     case Surface.ROTATION_180: 
      degrees = 270; 
      break; 
     case Surface.ROTATION_270: 
      degrees = 180; 
      break; 

     } 
     Log.i("DEGREES ARE WHAT??", Integer.toString(degrees)); 

//  mCamera.setDisplayOrientation(degrees); 
     mCamera.setDisplayOrientation(90); 

     mCamera.startPreview(); 
     mPreviewRunning = true; 

    } 

    // implements Observer 
    // captures a frame when the compass listener says it is appropriate 
    public void update(Observable observable, Object data) 
    { 
     mCaptureFrame = true; 
    } 
} 

請找出問題,因爲我懷疑有毛病的onPause(),它也當我黑色的相機屏幕後,按後退按鈕讓我異常

mCamera.setPreviewCallback(null); // (in onPause()) 

這裏是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="vertical" 
    android:background="#E6E5E6"> 
    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     android:gravity="center_horizontal"> 
     <TextView 
      android:id="@+id/tv_camera_url" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:maxLines="1" 
      android:textColor="@android:color/black" 
      android:text="URL goes here........" 
      android:layout_marginTop="3dp" 
      android:textStyle="bold"/> 
     <TextView 
      android:id="@+id/tv_speaknow" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:padding="3dp" 
      android:background="#ffffff" 
      android:layout_marginTop="3dp" 
      android:text="Speak Now" 
      android:textColor="#ff0000" 
      android:textStyle="bold"/> 
    </LinearLayout> 
    <RelativeLayout 
     android:id="@+id/rl_main" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"> 
     <SurfaceView 
      android:id="@+id/preview" 
      android:layout_width="480px" 
      android:layout_height="640px" 
      android:layout_alignParentBottom="true"/> 
     <Button 
      android:id="@+id/btn_take_picture" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:background="@drawable/camera" 
      android:layout_alignBottom="@+id/preview" 
      android:layout_centerHorizontal="true" 
      android:layout_marginBottom="5dp"/> 
    </RelativeLayout> 
</LinearLayout> 

回答

0

如果您第一次啓動應用程序,onPause真的不應該成爲問題。 但我會將創建等移動到onResume而不是onCreate。

我可以看到你的xml的surfaceview?

你只想要1張照片?

if(mPreviewRunning) { mCamera.stopPreview(); }

而大多數人從onResume開始預覽。試着把它移到那裏..

+0

我已編輯我的問題。 Xml現在在那裏。 – Khawar

相關問題