4

I use the Android camera to take a picture in my activity :

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, getImagePath());
startActivityForResult(intent, TAKE_PHOTO_CODE);

When I leave the camera app, the photo is saved in two places:

  1. At the path specified by the getImagePath() method (which is correct) ;
  2. Into the gallery. I don't whant that.

How can I prevent the photo to be saved into the gallery? And if I can't do it, how can I get the photo path in my onActivityResult() so I can delete it?

Blackwood
  • 4,504
  • 16
  • 32
  • 41
Thibault J
  • 4,336
  • 33
  • 44

2 Answers2

1

For Intent.ACTION_GET_CONTENT you can delete the gallery file (after copying it to your folder). Perhaps this would also work for MediaStore.ACTION_IMAGE_CAPTURE (with MediaStore.EXTRA_OUTPUT). I use the following code snippet for deleting the gallery file on return from Intent.ACTION_GET_CONTENT:

public String getRealPathFromURI(Uri contentUri) {
  String[] proj = { MediaStore.Images.Media.DATA };
  Cursor cursor = managedQuery(contentUri, proj, null, null, null);
  int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
  cursor.moveToFirst();
  return cursor.getString(column_index);
}

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  Uri u = data.getData();
  new File(getRealPathFromURI(u)).delete();
  ...
Mia Clarke
  • 8,134
  • 3
  • 49
  • 62
charles young
  • 2,269
  • 2
  • 23
  • 38
  • 2
    This method doesn't work with an intent Intent(MediaStore.ACTION_IMAGE_CAPTURE) because the intent (data, in your case) that is returned in onActivityResult is null ! – Jack' Nov 14 '16 at 13:31
0

Are you saving the image to the SD card somewhere in your getImagePath() code? If so, the Android Media Scanner is picking it up from that location to display in the gallery. You can create an empty file named .nomedia in the directory where your images are saved to have the Media Scanner ignore all the media files in that folder.

Marc Bernstein
  • 11,423
  • 5
  • 34
  • 32