Thibault J
Thibault J

Reputation: 4456

How to Remove duplicated picture in gallery when using android camera?

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?

Upvotes: 4

Views: 4626

Answers (2)

charles young
charles young

Reputation: 2289

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();
  ...

Upvotes: 1

Marc Bernstein
Marc Bernstein

Reputation: 11511

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.

Upvotes: 0

Related Questions