English 中文(简体)
PictureCallback.onPictureTaked从未调用
原标题:PictureCallback.onPictureTaken never called

我正在写一个需要用相机拍照的应用程序。当我试图拍一张真实的照片时,问题就出现了。以下是困扰我的代码:

    final ShutterCallback shutterCallback = new ShutterCallback() {
        @Override
        public void onShutter() {
            Log.d(TAG, "onShutter");
        }
    };

    final PictureCallback callback = new PictureCallback() {

        @Override
        public void onPictureTaken(byte[] data, Camera camera) {
            Log.d(TAG, "onPictureTaken - jpeg");
            try {
                //async task for storing the photo
                new SavePhotoTask(CameraView.this.ctx, data).execute();
            } catch (final SavePhotoException e) {
                //some exceptionhandling
            }
        }
    };
    this.camera = Camera.open();
    this.camera.setPreviewDisplay(surfaceHolder);
    final Camera.Parameters parameters = findBestParameters(w, h);
    this.camera.setParameters(parameters);
    this.camera.startPreview();
    Log.d(TAG, "takePicture now!");
    this.camera.takePicture(shutterCallback, null, callback);

在模拟器上,它似乎是可行的,但在我的手机上(摩托罗拉Defy-android 2.1)。

实际问题是:在电话中,图片回调从未发生“onPictureTaken”从未被调用。Shuttercallback被执行了,但另一个没有(我试过用raw而不是jpeg,也是一样)。

有人知道这个问题吗?我只是不知道模拟器现在的区别在哪里。我感谢你的帮助。

最佳回答

我终于去调试了这个问题。突然间,它起了作用,因为调试要慢得多:这是一个时间问题。回调需要一些时间才能调用。在调试手机时,有足够的时间完成拍照。。。

也不要过早调用Camera.stopPreview()和Camera.release()。

问题回答

我遇到了这个问题。经过多次调试,我终于意识到愚蠢的Camera对象在有机会调用回调之前就被垃圾收集了!

我通过创建一个对我正在使用的Camera对象的硬引用来修复它。我让它成为我的PictureTaker类的成员,在调用takePicture()之前设置它,然后在收到数据后在jpeg回调中将其置空。然后,我只需要确保我的PictureTaker对象本身不会得到gc d,这是我通过在进程的整个生命周期中将其保留在我的Application子类中来实现的。

这总是适用于我的Droid RAZR:

public class PictureTaker implements Camera.PictureCallback
{
  private Camera mCam;
  private MyApp theApp;

  public PictureTaker(MyApp app)
  {
     theApp = app;
  }

  public void takePicture()
  {
     try
     {
        mCam = Camera.open();
     }
     catch (Exception e)
     {
        System.out.println("Problem opening camera! " + e);
        return;
     }

     if (mCam == null)
     {
        System.out.println("Camera is null!");
        return;
     }

     try
     {
        SurfaceView view = MyApp.getPreviewSurface(); // my own fcn
        mCam.setPreviewDisplay(view.getHolder());
        mCam.startPreview();
        mCam.takePicture(null, null, this);
     }
     catch (Exception e)
     {
        System.out.println("Problem taking picture: " + e);
     }
  }

  public void onPictureTaken(byte[] data, Camera cam)
  {
     theApp.jpegPictureData(data);  // also my own fcn

     cam.stopPreview();
     cam.release();

     mCam = null;
  }
}

在我的情况下,它并没有调用特定设备上的picturetaken。问题的原因是在onResume()和oncreate()上打开了两次相机。





相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签