Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Camera takePicture function does not call Callback function

I am working on a custom Camera activity for my application. I was following the instruction from the Android Developers site here: http://developer.android.com/guide/topics/media/camera.html Everything seems to works fine, except the Callback function is not called and the picture is not saved. Here is my code:

public class CameraActivity extends Activity {
private Camera mCamera;
private CameraPreview mPreview;
private static final String TAG = "CameraActivity";

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.camera);

    // Create an instance of Camera
    mCamera = getCameraInstance();

    // Create our Preview view and set it as the content of our activity.
    mPreview = new CameraPreview(this, mCamera);
    FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
    preview.addView(mPreview);

    Button captureButton = (Button) findViewById(R.id.button_capture);
    captureButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.v(TAG, "will now take picture");
            mCamera.takePicture(null, null, mPicture);
            Log.v(TAG, "will now release camera");
            mCamera.release();
            Log.v(TAG, "will now call finish()");
            finish();
        }
    });        
}

private PictureCallback mPicture = new PictureCallback() {

    @Override
    public void onPictureTaken(byte[] data, Camera camera) {
        Log.v(TAG, "Getting output media file");
        File pictureFile = getOutputMediaFile();
        if (pictureFile == null) {
            Log.v(TAG, "Error creating output file");
            return;
        }
        try {
            FileOutputStream fos = new FileOutputStream(pictureFile);
            fos.write(data);
            fos.close();
        } catch (FileNotFoundException e) {
            Log.v(TAG, e.getMessage());
        } catch (IOException e) {
            Log.v(TAG, e.getMessage());
        }
    }
};

private static File getOutputMediaFile() {
    String state = Environment.getExternalStorageState();
    if (!state.equals(Environment.MEDIA_MOUNTED)) {
        return null;
    }
    else {
        File folder_gui = new File(Environment.getExternalStorageDirectory() + File.separator + "GUI");
        if (!folder_gui.exists()) {
            Log.v(TAG, "Creating folder: " + folder_gui.getAbsolutePath());
            folder_gui.mkdirs();
        }
        File outFile = new File(folder_gui, "temp.jpg");
        Log.v(TAG, "Returnng file: " + outFile.getAbsolutePath());
        return outFile;
    }
}

After clicking the Button, I get logs: "will now take picture", "will now release camera" and "will now call finish". The activity finishes succesfully, but the Callback function was not called during the

mCamera.takePicture(null, null, mPicture);

function (There were no logs from the mPicture callback or getMediaOutputFile functions) and there is no file in the location that was specified.

Any ideas? :) Much thanks!

like image 991
Tomáš 'Guns Blazing' Frček Avatar asked Oct 30 '13 15:10

Tomáš 'Guns Blazing' Frček


1 Answers

The call to mCamera.takePicture() is asynchronous. That means that the call completes immediately, but the actual picture taking and processing happens sometime later. However, you do this immediately upon return from the call to mCamera.takePicture():

Log.v(TAG, "will now release camera");
mCamera.release();
Log.v(TAG, "will now call finish()");
finish();

That means that you have released the camera resources and finished the Activity before the camera gets a chance to actually take the picture and call you back.

You need to move that code into the callback method onPictureTaken() so that you release the resources and finish the Activity after the callback occurs.

like image 121
David Wasser Avatar answered Sep 23 '22 14:09

David Wasser