自动拍摄照片时,takePicture失败,但按钮点击则有效

时间:2013-08-02 00:45:37

标签: android android-camera

所以我正在尝试创建一个应用程序打开后立即拍照的应用程序。当我运行代码时,我得到以下错误

Caused by: java.lang.RuntimeException: takePicture failed
    at android.hardware.Camera.native_takePicture(Native Method)
    at android.hardware.Camera.takePicture(Camera.java:1141)
    at android.hardware.Camera.takePicture(Camera.java:1086)
    at com.me.phonespy.StealthActivity.takePic(StealthActivity.java:110)
    at com.me.phonespy.StealthActivity.go(StealthActivity.java:116)

代码:

public class StealthActivity extends Activity implements PictureCallback {
Camera cam;
FrameLayout preview;        
CamPreview camPreview;
File pictureFile;
Bitmap bmp;
Matrix matrix;
Button pb;

protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_stealth);
    initCamPreview();
    pb= (Button) findViewById(R.id.button1);


}
@Override
public void onResume()
{
    super.onResume();
    pb.performClick();
}

@Override
public void onDestroy()
{

    releaseCameraAndPreview();

    super.onDestroy();
}

@Override
protected void onPause()
{
    super.onPause();

    preview.removeAllViews();
}
void initCamPreview()
{
    cam=openFrontFacingCameraGingerbread();
    cam.setDisplayOrientation(90);
    preview= (FrameLayout) findViewById(R.id.preview);
    camPreview= new CamPreview(this, cam);
    camPreview.setSurfaceTextureListener(camPreview);
    preview.addView(camPreview);
}

private void releaseCameraAndPreview() {
    if (cam != null) {
        cam.release();
        cam = null;
    }
}void takePic()
{
    cam.takePicture(null, null, this);

}

public void go(View view)
{
    takePic();

}


@Override
public void onPictureTaken(byte[] data, Camera camera)
{
      //handles saving image
     }

当我将cam.takePicture(null, null, this);放入onResumeonCreate时,我得到相同的takePicture失败但是当我按下按下go(View view) <按钮时,它会起作用并拍照/ p>

任何想法??

1 个答案:

答案 0 :(得分:0)

必须先开始预览才能拍照。你的startPreview()在哪里?另外,它们都在主线程中运行,当你进行初始化并注册回调时,你不能立即进行回调。