5

I am using the camera intent to launch the camera in my app but as soon as the intent gets fired the onActivityResult gets fired and I have not even taken a picture yet.

When I do take a picture, select it and return back to my activity the onActivityResult does not get called at all

here is how I launch the camera

PackageManager pm = getPackageManager();
    if (pm.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
        Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

        File tempDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),"Mobile Map");
        if (!tempDir.exists()) {
            if (!tempDir.mkdir()) {
                Toast.makeText(this,
                        "Please check SD card! Image shot is impossible!",
                        Toast.LENGTH_SHORT).show();
            }
        }

        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",Locale.US).format(new Date());
        File mediaFile = new File(tempDir.getPath() + File.separator+ "IMG_" + timeStamp + ".jpg");

        photoUri = Uri.fromFile(mediaFile);
        camera.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
        startActivityForResult(camera, CAMERA_REQUEST);
    } else {
        Toast.makeText(this,"This device does not have a rear facing camera",Toast.LENGTH_SHORT).show();
    }

Why is the onActivityResult only getting called after the camera intent launches?

tyczj
  • 71,600
  • 54
  • 194
  • 296
  • You said :"as soon as the (camera) intent gets fired the onActivityResult gets fired", but then "When I do take a picture ... onActivityResult does not get called at all". Which one is it? – Neoh May 16 '13 at 16:35
  • @Neoh both, it gets called when the intent is sent but when I actually want it to get called like when I take my picture it does not get called – tyczj May 16 '13 at 16:37
  • When `onActivityResult()` is called, what is the value of the resultCode parameter? – David Wasser May 16 '13 at 17:40
  • @DavidWasser resultCode is `0` and the requestCode is the code I give in the intent to launch the camera so it is coming from my camera intent – tyczj May 16 '13 at 17:47

1 Answers1

5

The problem was that in my manifest I had the activity set to singleInstance and apparently startActivityForResultdoes not like that

tyczj
  • 71,600
  • 54
  • 194
  • 296
  • 2
    Yes, if your activity has `launchMode="singleInstance"` that means that when you launch the camera activity, the camera activity will be launched into another task. You cannot communicate using `startActivityForResult()` between 2 activities that are not in the same task. That was actually where my line of thinking was going and why I asked the question about the result code. The result code of 0 is `RESULT_CANCELED` which indicates that your call to `startActivityForResult()` was immediately canceled for some reason. – David Wasser May 16 '13 at 20:42