takepicture hangs on Android 2.3.3

后端 未结 5 1700
感动是毒
感动是毒 2020-12-28 21:33

I have some codes of taking picture which works in Android 2.1 and 2.2. But these codes broke at Android 2.3. After spending time to fix this issue which went in vain, I wou

5条回答
  •  一整个雨季
    2020-12-28 22:17

    I ran into this issue with GB phones, and for me, it turned out to be because I was calling camera.startPreview() right after calling camera.takePicture(), and this was causing some thread locking in Android. The fix was to move camera.startPreview() into the callback passed into camera.takePicture(), such that it only got called once the picture data was in (example code below). This is of course only relevant if you're interested in restarting the preview after the picture is taken.

    // BAD BAD DON'T DO THIS!
    public void myTakePicture(Camera.PictureCallback callback) {
      mCamera.takePicture(null, null, null, callback);
      mCamera.startPreview();
    }
    
    // ...
    // The way that worked for me
    public void myTakePicture(final Camera.PictureCallback callback) {
      mCamera.takePicture(null, null, null, new Camera.PictureCallback() {
        @Override
        public void onPictureTaken(byte[] pictureData, Camera camera) {
          callback.onPictureTaken(pictureData, camera);
          mCamera.takePicture();
        }
      });
    }
    

    This not only made the ANR go away when calling takePicture, but also fixed a native crash on startPreview that was happening on some higher end phones (particularly a >=4.3 Nexus 5). Hope it helps!

提交回复
热议问题