Android:阅读障碍内容。同一文件返回两个不同的内容 uri ID,其中数字交换
使用选择器:
Intent intent = new Intent();
intent.setType("audio/mp3");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(Intent.createChooser(intent, context.getResources().getString(R.string.select)), GET_URI);
我得到一个音频文件。我获取该 Uri 并将其保存在本地数据库中以供稍后访问。我发现,对于所选的某首歌曲,当您尝试使用它创建 MediaPlayer 时,内容 uri 将返回 null 值。我尝试再次获取同一首歌的内容 uri,看看会发生什么,果然:
旧的、损坏的内容 uri: content://media/external/audio/media/2478
New,工作内容 uri:content://media/external/audio/media/2487
交换的最后两位数字的保存相同。在我的两次尝试中,获取此Uri的方法没有改变。这是一个已知问题吗?我的谷歌和搜索没有结果。但是,如果内容 uri 有阅读困难的习惯,这就是一个问题,因为除了重新获取歌曲的内容 uri 之外,没有真正的方法可以解决该问题,而这必须手动完成,因为无法引用歌曲的文件包含错误内容 uri 的路径。
有什么想法吗?
Using a chooser:
Intent intent = new Intent();
intent.setType("audio/mp3");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(Intent.createChooser(intent, context.getResources().getString(R.string.select)), GET_URI);
I get an audio file. I take that Uri and save it in a local database to access later. I found that for a certain song selected the content uri would return a null value when you tried to create a MediaPlayer using it. I tried to get the content uri for that same song again to see what would happen, and sure enough:
Old, broken content uri: content://media/external/audio/media/2478
New, working content uri: content://media/external/audio/media/2487
Identical save for the last two digits being swapped. The method to obtain this Uri has not changed between my two attempts. Is this a known problem? My google and SO searched yielded no results. But this is a problem if content uris have a habit of becoming dyslexic, because there is no real way to remedy the problem short of re-obtaining the song's content uri, which must be done manually since there is no way to reference the song's file path with the wrong content uri.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
他们中的一些人患有诵读困难!
内容提供者公开的一些字段被缓存。因此不能保证它们是新鲜的。由于您没有指定获取此 URI 的方式,因此我无法判断它是否是缓存值。
您可以查看代码并自行解决。
http://grepcode.com/file_/repository.grepcode.com/java/ext/com.google.android/android/1.5_r4/android/provider/MediaStore.java
Dyslexic some of them are!
Some of the fields exposed by the Content Providers are cached. And so they are not guaranteed to be fresh. Since you did not specify the means by which you obtained this URI, I cannot tell if it is a cached value.
You can look into the code and figure it out yourself.
http://grepcode.com/file_/repository.grepcode.com/java/ext/com.google.android/android/1.5_r4/android/provider/MediaStore.java