Android Camera startPreview失败

时间:2014-04-27 23:48:52

标签: android android-camera

我正在开发一款Android应用程序,需要在应用程序屏幕的一半内查看相机视图。我能够成功完成Android 4.0设备,但是当我尝试在我的Nexus One上运行它时它会崩溃,尽管minSDK版本已设置为2.2。这是我的相机预览课程。

package com.example.locationtest;
import java.io.IOException;
import android.content.Context;
import android.hardware.Camera;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
private static final String TAG = "error";
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);

}

public void surfaceCreated(SurfaceHolder holder) {
    // The Surface has been created, now tell the camera where to draw the preview.
    try {
        mCamera.setPreviewDisplay(holder);
        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.release();
}

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.
    Log.d("Function", "surfaceChanged iniciado");
    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());
    }
}
}

它提供以下logcat:

01-05 18:42:03.540: E/AndroidRuntime(583): FATAL EXCEPTION: main
01-05 18:42:03.540: E/AndroidRuntime(583): java.lang.RuntimeException: startPreview failed
01-05 18:42:03.540: E/AndroidRuntime(583):  at android.hardware.Camera.startPreview(Native     Method)
01-05 18:42:03.540: E/AndroidRuntime(583):  at     com.example.locationtest.CameraPreview.surfaceCreated(CameraPreview.java:31)
01-05 18:42:03.540: E/AndroidRuntime(583):  at android.view.SurfaceView.updateWindow(SurfaceView.java:543)
01-05 18:42:03.540: E/AndroidRuntime(583):  at android.view.SurfaceView.dispatchDraw(SurfaceView.java:348)
01-05 18:42:03.540: E/AndroidRuntime(583):  at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
01-05 18:42:03.540: E/AndroidRuntime(583):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
01-05 18:42:03.540: E/AndroidRuntime(583):  at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
01-05 18:42:03.540: E/AndroidRuntime(583):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
01-05 18:42:03.540: E/AndroidRuntime(583):  at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
01-05 18:42:03.540: E/AndroidRuntime(583):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
01-05 18:42:03.540: E/AndroidRuntime(583):  at android.view.View.draw(View.java:6883)
01-05 18:42:03.540: E/AndroidRuntime(583):  at android.widget.FrameLayout.draw(FrameLayout.java:357)
01-05 18:42:03.540: E/AndroidRuntime(583):  at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
01-05 18:42:03.540: E/AndroidRuntime(583):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
01-05 18:42:03.540: E/AndroidRuntime(583):  at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
01-05 18:42:03.540: E/AndroidRuntime(583):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
01-05 18:42:03.540: E/AndroidRuntime(583):  at android.view.View.draw(View.java:6883)
01-05 18:42:03.540: E/AndroidRuntime(583):  at android.widget.FrameLayout.draw(FrameLayout.java:357)
01-05 18:42:03.540: E/AndroidRuntime(583):  at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1862)
01-05 18:42:03.540: E/AndroidRuntime(583):  at android.view.ViewRoot.draw(ViewRoot.java:1522)
01-05 18:42:03.540: E/AndroidRuntime(583):  at android.view.ViewRoot.performTraversals(ViewRoot.java:1258)
01-05 18:42:03.540: E/AndroidRuntime(583):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
01-05 18:42:03.540: E/AndroidRuntime(583):  at android.os.Handler.dispatchMessage(Handler.java:99)
01-05 18:42:03.540: E/AndroidRuntime(583):  at android.os.Looper.loop(Looper.java:130)
01-05 18:42:03.540: E/AndroidRuntime(583):  at android.app.ActivityThread.main(ActivityThread.java:3683)
01-05 18:42:03.540: E/AndroidRuntime(583):  at java.lang.reflect.Method.invokeNative(Native Method)
01-05 18:42:03.540: E/AndroidRuntime(583):  at java.lang.reflect.Method.invoke(Method.java:507)
01-05 18:42:03.540: E/AndroidRuntime(583):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-05 18:42:03.540: E/AndroidRuntime(583):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-05 18:42:03.540: E/AndroidRuntime(583):  at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:0)

有同样的血腥问题,请试试这个:

mCamera.stopPreview();
mCamera.startPreview();
mCamera.setPreviewCallback(null);

答案 1 :(得分:-1)

使用Thread.Sleep(2000)添加新线程,然后开始预览, 这样摄像机将显示捕获的图像2秒钟并重新启动摄像机。

 Thread thread = new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        thread.sleep(2000);
                        mCamera.startPreview();
                    }
                    catch (InterruptedException e)
                    {

                    }
                }
            });
            thread.start();