Android - Passed value is null
<p>I've implemented the example code to use the camera API from <a href="http://www.vogella.com/articles/AndroidCamera/article.html" rel="nofollow">http://www.vogella.com/articles/AndroidCamera/article.html</a>.</p>

<p>I have the following IntentService</p>

<pre><code>package edu.ethz.Sens;
import android.app.IntentService;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.hardware.Camera.CameraInfo;
import android.os.Bundle;
import android.os.ResultReceiver; 
import android.util.Log;


public class PhotoService extends IntentService {

private static final String TAG = "Photo Service";

private final static String DEBUG_TAG = "MakePhotoActivity";
private Camera camera;
private int cameraId = 0;

private String retMsg;

// Is needed to send data from the service back to the activity
public ResultReceiver mResRec;  

public PhotoService() {
    super("PhotoService");
    // TODO Auto-generated constructor stub
}

public void onCreate() {
    super.onCreate();
    Log.d(TAG, "&gt;&gt;&gt;onCreate()");
}


@Override
protected void onHandleIntent(Intent intent) {

    mResRec = intent.getParcelableExtra("receiverTag");

    // do we have a camera?
                if (!getPackageManager()
                        .hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
                    Log.d(TAG,"No camera found");
                } else {
                    cameraId = findFrontFacingCamera();
                    camera = Camera.open(cameraId);
                    if (cameraId &lt; 0) {
                        Log.d(TAG,"No camera found");
                    }
                }

                PhotoHandler mPhotoHandler = new PhotoHandler(getApplicationContext());
                camera.takePicture(null, null, mPhotoHandler);
                camera.release();
                retMsg = mPhotoHandler.getFilename();


                Bundle b= new Bundle();
                b.putString("ServiceTag","photo");
                b.putString("Message", retMsg);
                mResRec.send(0, b);
}

private int findFrontFacingCamera() {
    int cameraId = -1;
    // Search for the front facing camera
    int numberOfCameras = Camera.getNumberOfCameras();
    for (int i = 0; i &lt; numberOfCameras; i++) {
        CameraInfo info = new CameraInfo();
        Camera.getCameraInfo(i, info);
        if (info.facing == CameraInfo.CAMERA_FACING_FRONT) {
            Log.d(DEBUG_TAG, "Camera found");
            cameraId = i;
            break;
        }
    }
    return cameraId;
}

public void onDestroy() {
    Log.d(TAG, "&gt;&gt;&gt;onDestroy()");
}
</code></pre>

<p>}</p>

<p>And this class to save the photo in the sdcard</p>

<pre><code>package edu.ethz.Sens;

import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.content.Context;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.os.Environment;
import android.util.Log;
import android.widget.Toast;

public class PhotoHandler implements PictureCallback {

private final Context context;

private static final String TAG = "Photo Handler";

public String filename;

public PhotoHandler(Context context) {
    this.context = context;
}

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

    File pictureFileDir = getDir();

    if (!pictureFileDir.exists() &amp;&amp; !pictureFileDir.mkdirs()) {

        Log.d(TAG, "Can't create directory to save image.");
        Toast.makeText(context, "Can't create directory to save image.",
                Toast.LENGTH_LONG).show();
        return;

    }

    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyymmddhhmmss");
    String date = dateFormat.format(new Date());
    String photoFile = "Picture_" + date + ".jpg";

    setFilename(pictureFileDir.getPath() + File.separator + photoFile);
    Log.d(TAG, filename);   

    File pictureFile = new File(filename);

    try {
        FileOutputStream fos = new FileOutputStream(pictureFile);
        fos.write(data);
        fos.close();
    //  Toast.makeText(context, "New Image saved:" + photoFile,
    //          Toast.LENGTH_LONG).show();
    } catch (Exception error) {
        Log.d(TAG, "File" + filename + "not saved: "
                + error.getMessage());
    //  Toast.makeText(context, "Image could not be saved.",
    //          Toast.LENGTH_LONG).show();
    }
}

private File getDir() {
    File sdDir = Environment
      .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
    return new File(sdDir, "CameraAPIDemo");
}

public String getFilename() {
    Log.d(TAG,"&gt;&gt;" + filename); 
    return filename;
}

public void setFilename(String name) {
    filename = name;
}
</code></pre>

<p>As you can see I'd like to get the filename of the saved photo back from the PhotoHandler (and the back to the activity) but the filename which I get by mPhotoHandler.getFilename() is alway null. It seems that mPhotoHandler.getFilename() will be executed before the onPictureTaken method is called. </p>

<p>What should I do?</p>

<p>Thanks in advance!</p>
