Android 某些设备不存在 Uri content://media/external/file
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21417211/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Uri content://media/external/file doesn't exist for some devices
提问by Bhiefer
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 });
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.
之后,我调用MediaScannerConnection.scanFile()
文件的父目录,因为这是我最常用的方法,即如何通知 MediaScanner 关于文件的删除以及如何刷新 MTP 内容。我尝试了我在 stackoverflow 上找到的所有其他方法(例如Android 如何使用 MediaScannerConnection scanFile),但没有什么比这更有效。
Btw. I use it only for APIs 11 and newer. External storage is certainly mounted.
顺便提一句。我只将它用于 API 11 和更新版本。外部存储肯定是挂载的。
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 ?
我有以下问题: 1. 您知道发生此异常的任何原因吗?我不想只是忽略异常。当它对大多数设备都很好时,我不想删除此代码。2.你知道一些新的可靠方法如何通知MediaScanner以及如何在删除某些文件时立即刷新MTP的内容吗?
回答by Günther Jena
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.
很可能它与设备上的缓存有关。捕获异常并忽略并不好,但我的问题已解决,并且似乎有效。