I insert an image via:
ContentValues values = new ContentValues();
values.put(Images.Media.TITLE, filename);
values.put(Images.Media.DATE_ADDED, System.currentTimeMillis());
values.put(Images.Media.MIME_TYPE, "image/jpeg");
Uri uri = this.getContentResolver().insert(Images.Media.EXTERNAL_CONTENT_URI, values);
But when I try to delete
File f = new File(imageURI);
f.delete();
The picture is no longer there but an empty placeholder is. Any ideas?
Android has a cache of sorts that keeps track of media files.
Try this:
It makes the MediaScanner service run again, which should remove the deleted image from the device's cache.
Looks like you also need to add this permission to your AndroidManifest.xml:
taken from: http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/content/ExternalStorage.html
This worked for me ( tested on HTC Desire, Android 2.3)
If you donot know the image-id as required in the answer of @manisha and @Muhammad Waqas Khan you can also delete by file name
[Update 2016-06-16]
Danger: this looks like "Only delete the item from the mediaDB". Since there is a SQLite-cascade-file-delete-trigger in the mediaDB the referenced file is also deleted if it exist.
To avoid this you have to change the column MediaStore.Images.Media.DATA to a non-existing filepath before calling delete.
Use this method getContentResolver().delete(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
MediaStore.Images.Media._ID + "=?", new String[]{ Long.toString(id) } );
As stated in the comments, the accepted answer uses a lot of memory. While MediaScannerConnection doesn't have a "deleteFile" method, just pass in the the old file path to the "scanFile" method once you've deleted the file. The media scanner will rescan and remove the media.
tested on N5. Android 4.4.
EDIT: Other have stated this doesn't work on 4.2
You can also delete the image from the Gallery by using:
getContentResolver().delete(imageUri, null, null);
Just make sure that 'imageUri' is the uri returned to you when you called insert.