Tuesday, 25 October 2011

Android: taking a picture and then deleting it from external storage

I've been playing with the bulit-in camera app today, launching it from within my app, taking a picture and then returning back to my calling activity (let's call it MyActivity) with an Intent containing a Uri instance pointing to the saved picture. I used the following guide to do this...

http://developer.android.com/guide/topics/media/camera.html#intents

... but with one difference: I didn't specify where to save the file, saving the picture instead in the default picture gallery location. Getting to the point of this post... say the Intent returned in the MyActivity.onActivityResult(int, int, Intent) method is named data and I get the Uri pointing to the picture out from data as follows:

Uri myPictureLocation = data.getData();

... Then, if I want to delete the saved picture once I am done with it, the thing to bear in mind is that myPictureLocation is not a file location but data pointed to by a content provider. So, you don't delete the picture as follows (like I tried!!)...

File myFile = new File(myPictureLocation.getPath());
myFile.delete();

...but instead you would chain the following method calls...

MyActivity.this.getContentResolver().delete(myPictureLocation, null, null);

No comments: