0

First I did look at other questions regarding this but they say to do what I am doing.

I am trying to launch the camera to take a picture like this

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), "My App");
                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();
        }

but when it comes back to onActivityResult the intent is null but the result is OK. I can go to the directory on the device and see that it was correctly saved too.

If I take out camera.putExtra(MediaStore.EXTRA_OUTPUT, photoUri); and just create a basic intent to launch the camera, the intent is not null on the return.

I would guess that photoUri is still valid and safe to store since the image is in the directory but why is the intent null on the return when I supply the uri?

tyczj
  • 71,600
  • 54
  • 194
  • 296
  • 2
    Be very careful about assuming where your photo will be stored. Many camera apps will not listen to your output location and store the photo wherever they want... I've had to deal with this before, it's no fun. – Steven Byle Apr 10 '13 at 18:42
  • @StevenByle then how do I know where it was stored if the intent always return null? Obviously that would be optimal to get it from the intent. – tyczj Apr 10 '13 at 18:53
  • My solution, which I'm not proud of, was to grab the latest picture added using a query through a `ContentProvider`. My app honestly can never be certain that's the picture that it just took, as there is a slim possibility another picture gets added in between the camera app finishing and returning to my app. Either way, the moral of the story is that you cannot rely on system/3rd party apps to save pictures to the directory you request or with the name you request. – Steven Byle Apr 10 '13 at 19:07

0 Answers0