Android相机startPreview失败

我正在研究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)

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

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

使用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();
链接地址: http://www.djcxy.com/p/90937.html

上一篇: Android Camera startPreview failed

下一篇: How to disable the airplane mode in android version 4.3 rooted device?