1

I am using the following intent:

Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
i.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, uri);

Basically i am using ACTION_IMAGE_CAPTURE intent to invoke the camera and save the taken image into the specified uri.

It works but at the same time the image is also saved with the default name.

Thus once i have snapped the picture, it is saved twice, both in the uri and in the default path and name.

How do i ensure that it is only saved in the specified uri?

Thanks In Advance, Perumal

Sherif elKhatib
  • 45,786
  • 16
  • 89
  • 106
perumal316
  • 1,159
  • 6
  • 17
  • 36

1 Answers1

1

You can take the ID or Absolute path of the gallery last image. And delete it.

It can be done like that:

/**
 * Gets the last image id from the media store
 * @return
 */
private int getLastImageId(){
    final String[] imageColumns = { MediaStore.Images.Media._ID, MediaStore.Images.Media.DATA };
    final String imageOrderBy = MediaStore.Images.Media._ID+" DESC";
    Cursor imageCursor = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, imageColumns, null, null, imageOrderBy);
    if(imageCursor.moveToFirst()){
        int id = imageCursor.getInt(imageCursor.getColumnIndex(MediaStore.Images.Media._ID));
        String fullPath = imageCursor.getString(imageCursor.getColumnIndex(MediaStore.Images.Media.DATA));
        Log.d(TAG, "getLastImageId::id " + id);
        Log.d(TAG, "getLastImageId::path " + fullPath);
        imageCursor.close();
        return id;
    }else{
        return 0;
    }
}

And to remove the file:

private void removeImage(int id) {
   ContentResolver cr = getContentResolver();
   cr.delete(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, MediaStore.Images.Media._ID + "=?", new String[]{ Long.toString(id) } );
}

This code was based on the post: Deleting a gallery image after camera intent photo taken

Community
  • 1
  • 1
Derzu
  • 7,011
  • 3
  • 57
  • 60
  • Appreciate that answer, do you know if this is a common work around? – Chris Sep 13 '12 at 00:44
  • @ChrisConway I'm not sure if it is the best way. But I believe that when you use the default Camera Intent the image will always be saved into gallery. Another solution is not use this Intent, you can create your own camera SurfaceView. – Derzu Sep 13 '12 at 12:25
  • Yeah, or like @SherifelKhatib said, just save it to the default directory. – Chris Sep 13 '12 at 19:41