3

I am trying to take a photo but it hangs when takePicture is called. No exception is generated. My code runs in an AsyncTask which is launced from a service. No activity is used and none MUST be used. I'm running this on Android 2.3.6. I can wait minutes and the onPictureTaken is never called.

Code:

import android.annotation.SuppressLint;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.os.AsyncTask;
import android.os.Build;

public class CameraAsyncTask extends AsyncTask<Void, Void, Boolean> implements Camera.PictureCallback
{
  private Camera camera;

  @Override
  protected Boolean doInBackground(Void... params)
  {
    this.camera = Camera.open();
    this.camera.takePicture(null, null, (PictureCallback) this);
    this.camera.release();
  }

  @Override
  protected void onPostExecute(Boolean succeeded)
  {
  }

  public void onPictureTaken(byte[] data, Camera camera)
  {
  }
}

Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest>

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <uses-permission android:name="android.permission.CAMERA" />

    <uses-feature
        android:name="android.hardware.camera"
        android:required="false" />
    <uses-feature
        android:name="android.hardware.camera.autofocus"
        android:required="false" />

</manifest>
4

2 回答 2

1

您需要在拍照前调用 this.camera.startPreview(),然后再调用 stopPreview()。此外,如果您调用 release,就像您在示例中所做的那样,您的回调将永远不会被执行

于 2015-05-30T01:57:04.743 回答
0

camera.takePicture 不能通过后台线程调用,必须通过主线程

于 2017-03-29T11:09:33.480 回答