9

I would like to capture an image from the camera and save it to the private app cache directory. I realize that I have to give the camera app permission to write to my private directory, so I added the FLAG_GRANT_WRITE_URI_PERMISSION flag.

What's happening is, that the camera app opens, I can take the picture, but when i click on the OK button, nothing happens. The camera app stays open. No log output. I guess it's because of a permission problem.

private void getCameraImage() {
    try {
        mTmpFile = File.createTempFile("tmp", ".jpg", getCacheDir());
        Uri imgUri = Uri.fromFile(mTmpFile);
        Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        i.putExtra(MediaStore.EXTRA_OUTPUT, imgUri);
        i.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
        // i.setData(imgUri); // if uncommented, i get an ActivityNotFound Exception
        startActivityForResult(i, REQUEST_CAMERA);
    } catch (IOException e) {
        Log.e(TAG, "getCameraImage()", e);
        Toast.makeText(this, "Something went wrong", Toast.LENGTH_SHORT).show();
    }
}

Any insights, on how I could correct that?

Edit:

When I change the directory to the public SD card, then it works fine.

mTmpFile = File.createTempFile("tmp", ".jpg", Environment.getExternalStorageDirectory());

Thanks Simon

SimonSays
  • 10,867
  • 7
  • 44
  • 59
  • 1
    If it was a permission issue, an error would show up in the LogCat. – PearsonArtPhoto Nov 15 '12 at 23:14
  • Not sure about that. Even when I remove the FLAG_GRANT_WRITE_URI_PERMISSION flag, I don't get any log output. Maybe the camera app checkes the permission, and just aborts the action if it can not write the file. – SimonSays Nov 15 '12 at 23:27

1 Answers1

1

Instead of passing the Uri to store the picture you can use http://developer.android.com/reference/android/hardware/Camera.html#takePicture(android.hardware.Camera.ShutterCallback, android.hardware.Camera.PictureCallback, android.hardware.Camera.PictureCallback, android.hardware.Camera.PictureCallback)

and get the content in your app memory. From there you will be able to do anything you want.

Do not forget that camera applications are using also the gallery and the gallery app is not allowed to check your private directory.

hasanghaforian
  • 13,858
  • 11
  • 76
  • 167
danysz
  • 580
  • 3
  • 11
  • Well, this is a very different way of getting the picture than what i intended to to. It's actually not an answer to the original question, but might still solve the problem. Since i am not working on that app anymore and therefore can't test the solution, i'll just go ahead and accept the answer - it's the only one anyways :-) – SimonSays Nov 19 '13 at 17:49
  • Thanks a lot for the appreciation and yes is not a direct answer to your question but as u said is the only possibility to do something that "belongs" only to your app and not to any other that is using media. I used this method by myself... Thanks – danysz Nov 19 '13 at 20:13
  • This solution was deprecated in API 21 – Douglas Fornaro Sep 17 '17 at 13:45