I'm writing an app that removes files that may or may not be listed in any one of the types of media libraries such as music or pictures. While I can use the MediaScannerConnection.scanFile
method to add files to the media library there doesn't seem to be any call to notify the service that the file has been removed. Sending it the path of the file that no longer exists doesn't result in the desired behavior either. How should I go about removing items from the library that no longer exist on the Android storage?

- 34,865
- 12
- 85
- 147
5 Answers
I was able to put a method together using bits and pieces from these two questions
- What is the String 'volumeName' argument of MediaStore.Audio.Playlists.Members.getContentUri referring to?
- How can I refresh MediaStore on Android?
Basically I just run a query on each one of the MediaStore types (Audio, Video and Images) selecting by path and deleting any records I find.
public static void RemoveAllForPaths(String[] paths, Context context)
{
private static final String[] FIELDS = { MediaStore.MediaColumns._ID, MediaStore.MediaColumns.DATA, MediaStore.MediaColumns.TITLE };
if(paths == null || paths.length == 0) return;
String select = "";
for(String path : paths)
{
if(!select.equals("")) select += " OR ";
select += MediaStore.MediaColumns.DATA + "=?";
}
Uri uri;
Cursor ca;
uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
ca = context.getContentResolver().query(uri, FIELDS, select, paths, null);
for(ca.moveToFirst(); !ca.isAfterLast(); ca.moveToNext()){
int id = ca.getInt(ca.getColumnIndex(MediaStore.MediaColumns._ID));
uri = ContentUris.withAppendedId(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, id);
context.getContentResolver().delete(uri, null, null);
}
ca.close();
// More of the same just setting the URI to Video and Images
}
I'm not entirely sure how safe this is to do but it's the only solution I've found so far and some initial testing seems to be working. I invite others to submit other answers if anyone has any further information on this approach or a better method for performing this functionality.

- 1
- 1

- 34,865
- 12
- 85
- 147
-
For path wildcards I added `select += MediaStore.MediaColumns.DATA + "like ?";` and appended `%` to my path string list. This returned all the files in a specific folder – Diederik Oct 05 '17 at 09:13
Easy as pie: whenever you add a file, let MediaStore
ContentProvider
knows about it using
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(fileToAddInMediaStore)));
For deletion: just use
getContentResolver().delete(Uri.fromFile(fileToDeleteFromMediaStore), null, null)
-
2
-
yes, it works for me. The only flaw I saw was that when I removed a file, the mediaStore is notified and recreated an empty (0 byte) file on some device. don't know exactly why, but I deleted it afterwards. – Pascal Jun 13 '13 at 18:58
-
As noted in http://stackoverflow.com/questions/15147536/how-to-trigger-mediascan-on-nexus-7 this will crash the app starting with Android 4.4. – Bernd S Nov 21 '13 at 05:44
-
@BerndS , I am using Intent.ACTION_MEDIA_SCANNER_SCAN_FILE. The link you gave is talking about Intent.ACTION_MEDIA_MOUNTED. Did you actually tried my solution on 4.4? – Pascal Nov 21 '13 at 08:31
-
@Pascal Sorry I thought you were talking about ACTION_MEDIA_MOUNTED. My bad. – Bernd S Nov 22 '13 at 04:46
-
1@Pascal Just tried your solution, and it works. Had to wait for my Nexus 7 to get the KitKat update. – Bernd S Nov 25 '13 at 22:37
-
So do you delete the file and then send the file path of the deleted file to the scan file? I have tried the command and am only able to add to the media library, not delete. – nomaam Dec 22 '13 at 18:57
-
Answer of Spencer Ruport is right, but you don't need to query and open a cursor in order to delete. So for one file that is music file the code is simple like that:
public void DeleteMP3FromMediaStore( Context context, String path )
{
Uri rootUri = MediaStore.Audio.Media.getContentUriForPath( path );
context.getContentResolver().delete( rootUri,
MediaStore.MediaColumns.DATA + "=?", new String[]{ path } );
}
P.S. I wanted to comment answer of Spencer Ruport but don't have enough reputation yet.

- 197
- 2
- 7
-
A lot simpler and cleaner way. Thanks. I was able to use this for bulk delete too, by mentioning a relevant where clause. – Sara Sep 10 '16 at 07:29
-
Seems like the most proper, direct and safest way. I hope it works on all configurations. – WindRider Sep 13 '16 at 09:33
The following works well for me. You can delete or add files using this.
MediaScannerConnection.scanFile(
context,
new String[]{fileToDelete, fileToAdd},
null, null);

- 1,110
- 2
- 13
- 24
-
2
-
media picker still shows the files as being there after deleting the file and then rescanning it – Someone Somewhere Dec 21 '15 at 15:42
The available method is to remove the item from library. This post is detailed expressed how to add into or remove from the Media Library. http://androidyue.github.io/blog/2014/01/19/scan-media-files-in-android/ Hopes this could help you.

- 952
- 10
- 11
-
1Hi, I like your post on github, but I can't seem to get the "Remove From Media Library" to work. I move files from the sdcard/Tumblr folder programatically (as I like them stored on the memory card instead of internal storage). When I do this, I need to update mediastore to let it know the image is "deleted" from the internal storage Tumblr folder. But, no matter what I do with your code, I can't get it to work. 2 question: (1) How do I specify the path for Tumblr in internal storage, (2) do i use EXTERNAL_CONTENT_URI or INTERNAL_CONTENT_URI. Thank you in advance. – KickAss Aug 08 '14 at 20:24