阅读本教程后,我编写了一个小相机应用程序:
http://www.brighthub.com/mobile/google-android/articles/43414.aspx
在模拟器中一切正常(好吧,没有相机,但至少没有错误;))
然后我试图让它在我的 HTC Wildfire S 上运行。但在启动后应用程序立即崩溃。
这是我的Activity的代码,也许有人有想法,如何解决这个问题:
public class CameraView extends Activity implements SurfaceHolder.Callback {
android.hardware.Camera mCamera;
boolean mPreviewRunning = false;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
getWindow().setFormat(PixelFormat.TRANSLUCENT);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.camera_surface);
SurfaceView mSurfaceView = (SurfaceView)findViewById(R.id.surface_camera);
SurfaceHolder mSurfaceHolder = mSurfaceView.getHolder();
mSurfaceHolder.addCallback(this);
mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
mCamera = android.hardware.Camera.open();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
if(mPreviewRunning) {
mCamera.stopPreview();
}
android.hardware.Camera.Parameters params = mCamera.getParameters();
params.setPreviewSize(width, height);
mCamera.setParameters(params);
try {
mCamera.setPreviewDisplay(holder);
} catch (IOException ex) {
ex.printStackTrace();
}
mCamera.startPreview();
mPreviewRunning = true;
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
mCamera.stopPreview();
mPreviewRunning = false;
mCamera.release();
}
android.hardware.Camera.PictureCallback mPictureCallback = new android.hardware.Camera.PictureCallback() {
@Override
public void onPictureTaken(byte[] data, android.hardware.Camera camera) {
}
};
}