Android 2.2中的相机崩溃

时间:2010-10-14 16:05:14

标签: android camera android-2.2-froyo

我已经在Android SDK上测试了我的应用程序,从1.5到2.2,我的活动中的摄像头代码运行正常。在2.1的设备上运行它也有效。但同样的应用程序无法与HTC Desire和Nexus One上的2.2兼容,应用程序崩溃。有什么想法可能会发生吗?

使用导入android.hardware.Camera;

在onCreate里面我有这个:

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();
    }
};

3 个答案:

答案 0 :(得分:9)

需要在获得最佳相机尺寸后设置相机的预览尺寸。以下是fix-

的详细信息和代码

http://code.google.com/p/android/issues/detail?id=7909

答案 1 :(得分:0)

问题是surfaceChanged方法传递的宽度和高度不是支持的预览尺寸。

因此,如果您想设置预览大小(parameters.setPreviewSize),则需要设置支持的预览大小。 getPreviewSize()方法返回可用的预览大小。所以你的surfaceChanged方法可以是这样的:

public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    Camera.Parameters parameters = camera.getParameters();

    Size pSize = camera.getParameters().getPreviewSize();
    Log.d(TAG, "Setting preview size: " + pSize.width + " x " + pSize.height);

    parameters.setPreviewSize(pSize.width, pSize.height);
    camera.setParameters(parameters);
    camera.startPreview();
}

您还可以使用android api版本5中提供的方法getSupportedPreviewSizes()获取支持的预览尺寸列表。

答案 2 :(得分:0)

你必须从相机拍摄照片,然后你必须裁剪。然后保存或设置。

您可以使用此代码。

                Intent intent   = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);                  
                mImageCaptureUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(),"connectR_" + String.valueOf(System.currentTimeMillis()) + ".jpg"));
                intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mImageCaptureUri);
                try {
                    intent.putExtra("return-data", true);                       
                    startActivityForResult(intent, PICK_FROM_CAMERA);
                } catch (ActivityNotFoundException e) {
                    e.printStackTrace();
                }

OnActivityResult。

case PICK_FROM_CAMERA:
            doCrop();               
            break;

现在裁剪使用 doCrop()

的图像
    final ArrayList<CropOption> cropOptions = new ArrayList<CropOption>();      
    Intent intent = new Intent("com.android.camera.action.CROP");
    intent.setType("image/*");        
    List<ResolveInfo> list = getPackageManager().queryIntentActivities( intent, 0 );        
    int size = list.size();        
    if (size == 0) {            
        Toast.makeText(this, "Can not find image crop app", Toast.LENGTH_SHORT).show();         
        return;
    } else {
        intent.setData(mImageCaptureUri);            
        intent.putExtra("outputX", 200);
        intent.putExtra("outputY", 200);
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
        intent.putExtra("scale", true);
        intent.putExtra("return-data", true);

        if (size == 1) {
            Intent i        = new Intent(intent);
            ResolveInfo res = list.get(0);              
            i.setComponent( new ComponentName(res.activityInfo.packageName, res.activityInfo.name));                
            startActivityForResult(i, CROP_FROM_CAMERA);
        } else {
            for (ResolveInfo res : list) {
                final CropOption co = new CropOption();                 
                co.title    = getPackageManager().getApplicationLabel(res.activityInfo.applicationInfo);
                co.icon     = getPackageManager().getApplicationIcon(res.activityInfo.applicationInfo);
                co.appIntent= new Intent(intent);                   
                co.appIntent.setComponent( new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
                cropOptions.add(co);
            }

            CropOptionAdapter adapter = new CropOptionAdapter(getApplicationContext(), cropOptions);

            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("Choose Crop App");
            builder.setAdapter( adapter, new DialogInterface.OnClickListener() {
                public void onClick( DialogInterface dialog, int item ) {
                    startActivityForResult( cropOptions.get(item).appIntent, CROP_FROM_CAMERA);
                }
            });

            builder.setOnCancelListener( new DialogInterface.OnCancelListener() {
                @Override
                public void onCancel( DialogInterface dialog ) {

                    if (mImageCaptureUri != null ) {
                        getContentResolver().delete(mImageCaptureUri, null, null );
                        mImageCaptureUri = null;
                    }
                }
            } );

            AlertDialog alert = builder.create();

            alert.show();
        }
    }