Android Camera takePicture function does not call Callback function

13,371

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.

Share:
13,371
Tomáš 'Guns Blazing' Frček
Author by

Tomáš 'Guns Blazing' Frček

Updated on July 21, 2022

Comments

  • Tomáš 'Guns Blazing' Frček
    Tomáš 'Guns Blazing' Frček almost 2 years

    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!

  • Tomáš 'Guns Blazing' Frček
    Tomáš 'Guns Blazing' Frček over 10 years
    Well that was simple...i'm kindda surprised I couldn't find a similar question here. Anyway, that did it, thank you very much :)
  • IgorGanapolsky
    IgorGanapolsky over 8 years
    I am not calling mCamera.release() nor finish(), and still having this problem. So something else may be going on.
  • IgorGanapolsky
    IgorGanapolsky over 8 years
    Why are you passing nulls to takePicture method params?
  • David Wasser
    David Wasser over 8 years
    @IgorGanapolsky maybe you should open a new question instead of adding a comment to this answer. Obviously your situation is different. The original poster seemed satisfied with this answer.