Android:Camera.takePicture回调并不总是触发

时间:2012-05-14 10:44:16

标签: android camera photo image

可能有90%的时间,回调将无法触发。 因此我的照片永远不会被保存。

我做错了什么?

   @Override
public boolean onTouchEvent(MotionEvent event)
{
    boolean result = super.onTouchEvent(event);

       int action = event.getAction();
       if(action == MotionEvent.ACTION_DOWN)
       {
           takePicture();

           this.finish(); // ERROR IS HERE. Closing down before callback is done.

       }    
       return result;
}

private void takePicture() {
    if (mCamera != null)
        mCamera.takePicture(shutterCallback, null, jpegCallback);     
}

ShutterCallback shutterCallback = new ShutterCallback() {
      public void onShutter() {

          AudioManager meng = (AudioManager) getBaseContext().getSystemService(Context.AUDIO_SERVICE);
            int volume = meng.getStreamVolume( AudioManager.STREAM_NOTIFICATION);

            if (volume != 0)
            {
                    MediaPlayer _shootMP = MediaPlayer.create(getBaseContext(), Uri.parse("file:///system/media/audio/ui/camera_click.ogg"));
                    _shootMP.start();
            }
      Toast.makeText(CameraActivity.this, "Picture Taken", Toast.LENGTH_SHORT).show();
      }
};

PictureCallback rawCallback = new PictureCallback() {
      public void onPictureTaken(byte[] _data, Camera _camera) {
        // TODO Do something with the image RAW data.
          int test = 1;
      }
};

PictureCallback jpegCallback = new PictureCallback() {
  public void onPictureTaken(byte[] _data, Camera _camera) {
      File photo=new File(Environment.getExternalStorageDirectory(), "photo1.jpg");

      if (photo.exists()) {
            photo.delete();
      }

      try {
        FileOutputStream fos=new FileOutputStream(photo.getPath());

        fos.write(_data);
        fos.close();
      }
      catch (java.io.IOException e) {
        Log.e("PictureDemo", "Exception in photoCallback", e);
      }



      SqlDB.SavePhoto(1, _data);      
  }
};

1 个答案:

答案 0 :(得分:2)

你可以在任何函数rawCallback或jpegCallback中调试和检查它。您是否为相机设置了jpeg格式。我认为根据该回调函数将被调用。所以它应该进入rawCallback或jpegCallback。你可以尝试一次。

mCamera.takePicture(shutterCallback,rawCallback,jpegCallback);

如果它失败,那么它可能会在某处您再次打开相机。在这种情况下,我认为你的相机参数将被重置,并可能返回原始回调而不是jpeg

实际上我认为如果相机没有崩溃并且它不是空的那么唯一的情况仍然是您使用默认设置重置相机或者您再次打开相机..