Android Camera startPreview failed
I am working on an Android app which needs a camera view in half of the app screen.I was able to do it successfully for an Android 4.0 device but it crashes when I try to run it on my Nexus One,although the minSDK version has been set to 2.2.Here's my camera preview class.
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());
}
}
}
It gives the following 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/90938.html
上一篇: Android相机错误5001