Uri content://media/external/file doesn't exist for some devices

Bhiefer picture Bhiefer · Jan 28, 2014 · Viewed 165.9k times · Source

I have an issue with some devices. I cannot replicate it on any device but I have quite a lot of crash reports reported by some users.

It is this exception:

java.lang.IllegalArgumentException: Unknown URL content://media/external/file
    at android.content.ContentResolver.delete(ContentResolver.java:1024)

I use this:

context.getContentResolver().delete(MediaStore.Files.getContentUri("external"), MediaStore.Files.FileColumns.DATA + "=?", new String[] { path });

After that I call MediaScannerConnection.scanFile() on file's parent directory, because it is imho most functional way how to notify MediaScanner about deletion of file and how to refresh MTP content. I tried all other ways that I have found here on stackoverflow (e.g.Android How to use MediaScannerConnection scanFile) but nothing is working as well as this.

Btw. I use it only for APIs 11 and newer. External storage is certainly mounted.

I have these questions: 1. Do you know any reason why this exception occurs ? I don't want just to ignore the exception. And I don't want to remove this code when it works quite good for most devices. 2. Do you know some new reliable method how to notify MediaScanner and how to refresh immediately content of MTP when some file is deleted ?

Answer

Günther Jena picture Günther Jena · Nov 2, 2016

Most probably it has to do with caching on the device. Catching the exception and ignoring is not nice but my problem was fixed and it seems to work.