Android 2.2 中的相机崩溃 [英] Camera crashes in Android 2.2

查看:31
本文介绍了Android 2.2 中的相机崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在从 1.5 到 2.2 的所有 Android SDK 上测试了我的应用程序,并且我的活动中的相机代码工作正常.在具有 2.1 的设备上运行它也可以正常工作.但同一应用程序在 HTC Desire 和 Nexus One 上无法与 2.2 一起使用,并且应用程序崩溃.任何想法为什么它们可能会发生?

I have tested my application on the Android SDK on everything from 1.5 to 2.2 and the camera code in my activity works fine. Running it on a device with 2.1 is also working. But the same app does not work with 2.2 on HTC Desire and Nexus One and the app crashes. Any ideas why they might be happening?

我正在使用 import android.hardware.Camera;.

在 onCreate 里面我有这个:

Inside onCreate I have this:

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

而回调是

SurfaceHolder.Callback surfaceCallback = new SurfaceHolder.Callback() {

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        camera.stopPreview();
        camera.release();
        camera = null;
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        camera = Camera.open();
        try {
            camera.setPreviewDisplay(previewHolder);
        } catch (Throwable t) {
            Log.e(TAG, "Exception in setPreviewDisplay", t);
            Toast.makeText(CameraActivity.this, t.getMessage(),
                    Toast.LENGTH_LONG).show();
        }
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
            int height) {
        Camera.Parameters parameters = camera.getParameters();
        parameters.setPreviewSize(width, height);
        camera.setParameters(parameters);
        camera.startPreview();
    }
};

推荐答案

需要在得到最优相机尺寸后设置相机的预览尺寸.这是修复的详细信息和代码-

Need to set the preview size of the camera after getting the optimal camera sizes. Here are the details and the code for the fix-

http://code.google.com/p/android/问题/详细信息?id=7909

这篇关于Android 2.2 中的相机崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆