onActivityResult getting called as soon as camera

2019-04-29 14:44发布

问题:

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?

回答1:

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